There are two properties of ECMAScript |
Data properties and Accessor properties |
There are 4 characteristics of data attributes |
Configurableenumerablewritablevalue the default value for the first three values is false for example object.defineproperty (person, "name", {writable:false, value: "Niko"}); Once the attribute is defined as not configurable, it cannot be changed back to configurable. |
Properties of the Read attribute |
var descriptor = object.getownpropertydescriptor (book, "_year") descriptor.valuedescriptor.configurable |
Creating an object using the constructor pattern |
function Person (name,age,job) {this.name = name; This.age = age; This.sayname = function () {alert (this.name)}} |
Creating objects in prototype mode |
function person () {}person.prototype.name = "Jeff"; Person.prototype.age = 28 The advantage is that all object instances can share the properties and methods it contains, Person.prototype point to the prototype object and Person.prototpe.constuctor to the person |
Judging type |
Person.prototype.isPrototypeOf (Person1)//True object.getprototypeof (person1) = = Person.prototype); True |
Determine if a property is in the prototype, not in the instance |
function Hasprototypeproperty (object, name) {return!object.hasownproperty (name) && (name is Object); |
Gets all enumerable instance properties on an object |
Object.keys () method |
If you want to get all the instance properties, whether it's enumerable or not, you can use the method |
Object.getownpropertynames () DVar keys = Object.getownpropertynames (Person.prototype);//"Constructor name age job Sayname |
You cannot determine the type of an object by using constructor |
var friend = new person () friend instanceof Object//truefriend instanceof person//truefriend.constructor = = person//FAL Sefriend.constructor = = Object//True |
What links are passed between instances and prototypes |
It's just a pointer, not a copy. |
Add a method to the prototype of the native object |
String.prototype.startWith = function (text) {return This.indexof (text) = = 0}var msg = "Hello World" msg.startwith ("Hell O "); |
|
|
|
constructor mode is used to define instance properties, while prototype mode is used to define methods and shared properties function person (name, age, Job) { this.name = name; &nbs P This.age = Age; this.job = job;} Person Protytype = { constructor:person, sayname:function () { a Lert (this.name); }} |
Dynamic Prototyping Mode |
function person (name, age, Job) {this.name = name this.age = age; This.job = job; if (typeof this.sayname! = "function") {Person.prototype.sayName = function () {}; }} |
Parasitic constructor mode |
Waiting to be mended |
Secure constructor Mode |
Waiting to be mended |
Inherited |
Waiting to be mended |