Object.create influence () on the prototype object attributes and the __proto__

Below are some details of the information to determine whether a modification of logic, affix Projects:

clipboard.png

So the question is, how all of you to make the brain to determine whether an image is modified.

A, see the following two questions about the Object.create classic surfaces () of

Question 1

var a = { name: 'kelen' };
var b = Object.create(a);
b.name = 'boke';
console.log(a.name);  

Problem 2

var a = { person : { name: 'kelen' } };
var b = Object.create(a);
b.person.name = 'kobe';
console.log( a.person.name ); 

Think

Why the name attribute of obj2 first modification, name obj1 output is one, while the second one but you can change it?

Second, explain: Object.create function

Object.create () method to create an object that has the specified prototype and a number of specified property.

grammar

Object.create(proto, [ propertiesObject ])

parameter

proto

An object that is created as a new object's prototype.

propertiesObject

Optional. The parameter object is a set of attributes and values, the attribute name of the property of the object name of the object will be newly created, the value of a property descriptor (Object.defineProperties these attributes with the structure descriptors () of the second parameter). . Note: This parameter object can not be undefined, the only other enumerable properties of the object itself has to be effective, that is to say on the prototype chain of the object attribute is invalid.

抛出异常

If proto argument is not null value or an object, throw a TypeError exception

return value

Having a specified internal prototype and contains the specified attributes (if any) of the new object.

Third, the title Detailed

Object.prototype property indicates the object's prototype object Object.

__proto__Property represents an object Objectprototype, the prototype can be an object of value may also be null, for example, Object.prototype.__proto__it isnull

clipboard.png

Analysis from the above examples, when var b = Object.create (a); the time is not equal to b = a, actually

b.__proto__ ==> a; 

b.prototype ==> a.prototype;

When our b.name = "kobe"time is actually a new property in the object b, and the assignment, but b.name = "kobe"before printing console.log(b.name);output time "kelen", because to find a name property in the prototype chain, so you can get to a value of

So the following b.name = "kobe"does not change a property's name, if you want to achieve the final value a.name kobe, you can write like this b.__proto__.name = "kobe";

While the second case,

b.person.name = "kobe"It can change the value a.person.name because, b.person is a a.person object reference, when b.person.name assignment, in fact, be modified to a.person.name, give a simple examples of the type of reference

var a = { name: "kelen" };
var b = a;
b.name = "kobe";
console.log(a.name); // kobe

Fourth, the Detailed Project Issues

Question 1

var a = { name: 'kelen' };
var b = Object.create(a);
b.name = 'boke';
console.log(a.name);  // kelen

Problem 2

var a = { person : { name: 'kelen' } };
var b = Object.create(a);
b.person.name = 'kobe';
console.log( a.person.name ); // kobe
  

Item code

//原始接口拿到的福利信息, //图片列表对象
originInfo={
    welfareName:'中影国际',
    imageList:[
        {
            id:'5157',
            wIFImgUrl:'http://tcw-wsq.b0.upaiyun.com/2016/07/12/34/20160713165313449.jpg'
        },
        {
            id:'5158',
            wIFImgUrl:'http://tcw-wsq.b0.upaiyun.com/2016/07/14/34/20160732112832397.jpg'
        }
    ]
};

//$scope.welfareInfo=originInfo;  //no,完全行不通
//$scope.welfareInfo=Object.create(originInfo); //no,不可以
var originInfoCy=JSON.stringify(originInfo);  //stringify用于从一个对象解析出字符串
$scope.welfareInfo=JSON.parse(originInfoCy);  //parse 用于从一个字符串中解析出json 对象

For this problem the correct approach is to originInfo transition object to a string, the string is not a reference type , and then converted to an object, it is the perfect solution to the problem Lala. . .

This article is reproduced in: ape → 2048 https://www.mk2048.com/blog/blog.php?id=hhkk1kbbj2j

Guess you like

Origin www.cnblogs.com/baimeishaoxia/p/12625833.html