最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Javascript objects parsed vs created - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Aug 13, 2013 at 9:59 Antti Haapala -- Слава Україні 134k23 gold badges292 silver badges344 bronze badges asked Aug 13, 2013 at 9:42 AnriAnri 6,2653 gold badges40 silver badges61 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 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 mon 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.

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 plex, you can run into severe problems.

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"}');

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.

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.

your statement

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

is just a inefficient way to do this:

  var p2 = {Name:'John'};
发布评论

评论列表(0)

  1. 暂无评论