7

What is the practical difference between p and p2 objects here:

var Person = function(name) { this.Name=name; } var p = new Person("John"); var p2 = JSON.parse('{"Name":"John"}'); 

What are the cases when I would better create new Person() and copy values from parsed JSON, rather than use that parsed JSON object as I would use the instance of Person?

PS. Let's say I got JSON string from WebSocket and I will have to parse it anyway.

6 Answers 6

6

The difference between p and p2 is that the internal prototype of p2 is the Object.prototype, whereas the prototype of p is the Person.prototype; for the p you can add common methods or attributes to all Person objects by adding methods to the Person.prototype.

Thus you could have the JSON format to have fields such as "givenName", "surname" "title", and by using the new Person approach, you could have a method:

Person.prototype.getFullName = function () { return this.title + " " + this.givenName + " " + this.surname; } 

that is readily available all Persons derived using the constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, fixed json string
2

The main difference is that p is an object, that is an instance of Person while p2 is a "plain" object, that is just an instance of Object.

When is this difference important?

1) accessing prototype properties:

var Person = function(name) { this.Name=name; } Person.prototype.getName = function () { return this.Name; }; p.getName() //works fine p2.getName() //Error since getName is not defined 

Or:

console.log(p.constructor) //Person console.log(p2.constructor) //Object 

2) using the instanceof operator:

p instanceof Person //true p2 instanceof Person //false 

3) everything that has to do with inheritance

All three points can essentially be traced back to the prototype chain, that looks like this for both ways:

p --> Person --> Object p2 --> Object 

Now, since you have this constructor function I would suggest you always use it, because it can get quite messy if you mix Person objects with plain objects. If you really just want an object, that has a Name property, you would be fine in both ways, but as soon as it gets a little bit more complex, you can run into severe problems.

1 Comment

Thanks for extended answer.
2

The main difference is that they have different prototype chains:

p.constructor -> function (name) { this.Name=name; } (the Person function) chain: p -> Person -> Object p2.constructor -> function Object() { [native code] } (the standard object constructor) chain: p2 -> Object 

If you need the proper prototype chain (e.g. have several methods on Person.prototype you want to inherit), use the new Person() construct. If you have a JSON String (e.g. from an ajax call), where you have all your data in (and don't need Person as prototype), use the JSON.parse, otherwise use the good ol' plain:

var p2 = {Name:'John'}; 

sidenote:

var p2 = JSON.Parse("{Name:'John'}"); 

is wrong, it needs to be:

var p2 = JSON.parse('{"Name":"John"}'); 

1 Comment

just as a sidenote: the prototype chain for p doesn't include Function.
1

The parse version basically does the same with the added overhead of breaking the text up and translating it ( which isn't slow ). I would consider it better programming using the first method, you don't want raw JSON hanging around your code :)

JSON.parse will create a very basic object from the data you give it, there's no problem with that

Using the first method allows you to define and extend the object before-hand, giving it methods and attributes allowing for better use of the data it contains.

1 Comment

I would appreciate emphasizing the last paragraph. Parsing a JSON string will not instantiate an object of a specific class (so not getting any prototypal methods). It will only create a basic objects with applied properties.
1

Simply put, p is a Person object and p2 is just an object literal. They both have different prototypes, name the Person prototype for p and the Object prototype for p2.

The first case defines a "class" with which you create new objects, using the "class" as a constructor for the new object.

The second case creates an object literal and assigns values to the specified keys.

Comments

0

your statement

var p2 = JSON.Parse("{Name:'John'}"); 

is just a inefficient way to do this:

 var p2 = {Name:'John'}; 

2 Comments

Yes, but let's say i got JSON string from WebSocket
@Anri that's fine... I am sure you would not code it that way... my point was to remove JSON to make the answer self-explanatory

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.