Object Properties Object-Oriented Design

1. Object Properties

Person = {} the let;
Object.defineProperty (Person, "name", {
Configurable: to true, // delete delete an attribute indicating whether by so redefining attributes, can modify the properties
enumerable: true, // for indicating whether by -in return property cycle
writable: true, // indicate whether it can modify the value of the property
value: "xujiang" property value //
})
when / * create a new property in the call Object.defineProperty () method, if not before the specified three attribute field, the default value is false, if the modification is defined properties, does not have this limitation * /

2. Access properties // GET / SET
the let = {Person name: "xujaijgn", year:. 11};
Object.defineProperty (Person, "the _name", {
GET: function () {
return this.name
},
// when defined cause set, the set value of a property other property changes
sET: function (newValue) {
this.name = newValue;
this.year 12 is =;
}
})

// 定义多个属性
Object。defineProperties(book, {
_year: {
writable: false,
value: 2001
},
year: {
get: function(){
return _year
},
set: function(newValue){
this._year = newValue;
}
}
})

2. Analyzing the object attribute exists in hasOwnProperty

determining whether there is a property in the object

determining whether there hasOwnProperty property instance

delete delete object attributes 

 

example:

function Person(){

}

Person.protype.name = 'Wong';

was person = new Person ();

console.log (person.name) // Faye

console.log(name in person )//true

console.log(person.hasOwnProperty('name')) //false

person.name = 'Daniel';

console.log(person.hasOwnProperty('name')) //true;

3. JS thoroughly get to know the prototype, __ proto__ and constructor 

new operator function is called, and then create a new object, and became a function of the context (that is, inside the function at this time this is a pointer to the new object is created, which means we can pass this parameter initialization within the constructor function value), and returns a reference to the new object

 __proto__ Property, which is the object unique , see __proto__attributes are of an object point to an object , which points to their prototype object (to be appreciated that the parent object)

prototype property of its role is to include all the properties and methods can be made to share examples of a particular type, that is, let the function instantiated object who can find common properties and methods.

constructor property is subject only have , it is from an object point to a function , meaning that points to the object's constructor , each object has a constructor (itself owned or inherited, inherited to combine __proto__the properties view will more clearly, as shown below)

4. prototype chain

var Foo=function(){} var foo=new foo();

foo.__proto__===Foo.prototype

Foo.prototype.__proto__===Object.protype;

Object.protype.__proto__===null;

 

 

 

 

 

Reference blog address:

https://blog.csdn.net/cc18868876837/article/details/81211729

http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html

https://juejin.im/post/5d8c86d06fb9a04e172071a0

 

Guess you like

Origin www.cnblogs.com/wayOfHero/p/12565774.html