Reading notes-JS advanced Programming-The sixth chapter object-oriented programming

Source: Internet
Author: User

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

Reading notes-JS advanced Programming-The sixth chapter object-oriented programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.