JS Elevation Note--Create object

Source: Internet
Author: User

1. Factory mode

Ex

function Createperson (name, age, Job) {var o = new Object (); o.name = Name;o.job = job; o.sayname = function () {alert (th Is.name);}; return o;} var person1 = Createperson ("Nicholas", "Software Engineer"), var person2 = Createperson ("Greg", "Doctor");

Creates an object by returning an internal object;

2. Constructor mode

Ex

function person (name, age, Job) {this.name = name; this.age = Age;this.job = Job;this.sayname = function () {alert (This.nam e);};} var person1 = new Person ("Nicholas", "Software Engineer"), var person2 = new Person ("Greg", "Doctor");

As a constructor, create a new object, assign the scope of the constructor to the new object, execute the code in the constructor, add properties and methods, and return the new object.

The downside is that the same thing that the same method function does requires a number of different or unequal functions to be produced.

3. Prototype mode

function person () {}person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "Software Engineer"; Person.prototype.sayName = function () {alert (this.name);}; var person1 = new Person ();p erson1.sayname (), var person2 = new Person ();p erson2.sayname (); alert (Person1.sayname = = Person2.sayname);  /true

All instances share the properties and methods of the prototype

The prototype object understands:

Prototype's constructor property points to the new build function

The prototype property of the build function points to the prototype prototype

New object's [[Prototype]] property points to the prototype Prototype

Person.prototype.isPrototypeOf (Person1)//true

Person.prototype.isPrototypeOf (Person2)//true

Object.getprototypeof (person1) = = Person.prototype

Object.getprototypeof (person1). Name//access

Here the access first accesses whether the object has this property, if none, and then accesses the prototype to have the attribute, and then access the higher-level prototype, and so on.

hasOwnProperty can detect whether a property exists in an instance or in a prototype, and this method returns true only if the given property exists in the object instance.

JS Elevation Note--Create object

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.