473,657 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perferred way to create object

Method 1:

var Person = {

name: "",
sayHello: function() {
....
}

}
Method 2:

function Person(name) {
this.name = name;
}

var p = new Person("john");


While both is okay, I see a trend more and more people is using method
1, any reason?

Jul 4 '07 #1
2 1214
Lee
howa said:
>
Method 1:

var Person = {

name: "",
sayHello: function() {
...
}

}
Method 2:

function Person(name) {
this.name = name;
}

var p = new Person("john");
While both is okay, I see a trend more and more people is using method
1, any reason?
The two methods accomplish different things.

Method 1 creates one variable named Person which is an
instance of an anonymous Object initialized with literal
data. If you have all of the data required to instantiate
all the Objects you're going to need, this is simple.

Method 2 creates a constructor of Person Objects, which
is then used to instantiate a new Person and assign a
reference to it to a variable named p.
The same constructor could also be used to construct as
many more Person Objects as you like, dynamically.
--

Jul 4 '07 #2
The second one is easier to debug due to:

the Person instance
* has a constructor property
* is an instanceof Person

Person.prototyp e.isPrototypeOf ( p ) is true.

Method 1 is popular, though, you're right! Closures are popular, too.
I really feel this is overused, especially in YUI.

I feel that using an object literal can be less explicit because
everything is an object, whereas a Person is a more specific type of
object.

One possible reason that might make method 1 (object literal approach)
favorable would be for a simple value object that is used right away
(such as Person).

Finally, If you can't have more than one Person instance, but want to
have a constructor, you can perform a simple entryCheck to the
constructor to make sure it's your factory getInstance method. Some
people might think this is complicated, but I actually like this
approach.

On Jul 4, 1:18 pm, Lee <REM0VElbspamt. ..@cox.netwrote :
howa said:


Method 1:
var Person = {
name: "",
sayHello: function() {
...
}
}
Method 2:
function Person(name) {
this.name = name;
}
var p = new Person("john");
While both is okay, I see a trend more and more people is using method
1, any reason?

The two methods accomplish different things.

Method 1 creates one variable named Person which is an
instance of an anonymous Object initialized with literal
data. If you have all of the data required to instantiate
all the Objects you're going to need, this is simple.

Method 2 creates a constructor of Person Objects, which
is then used to instantiate a new Person and assign a
reference to it to a variable named p.
The same constructor could also be used to construct as
many more Person Objects as you like, dynamically.

--

Jul 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2071
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
2
2117
by: Just D. | last post by:
All, Do we have a simple way to Create an object on the fly knowing just an object type? The usual design-time way is to write a code something like this: CObjectType obj = new CObjectType(); That's simple. But to create any object knowing its object type on the fly is looking like a problem. I'll try to explain the idea.
18
2708
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt http://ucsu.colorado.edu/~bethard/py/pep_create_statement.html In this post, I'm especially soliciting review of Carl Banks's point (now discussed under Open Issues) which asks if it would be better to have the create statement translated into:
27
3774
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB updates the same data in all other four tables in the right places. I know it would be possible by using the ForeignKeyConstraint object. I have created the tables using the DataSet Visual Tool and I know it doesn't create any ForeignKeyConstraint obj....
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.