Creation of objects

Source: Internet
Author: User

/*
* Factory mode
*
*/
Factory mode: Use functions to encapsulate the details of creating objects with specific interfaces

function Createperson (name,age,job) {varnew  Object (); O.name=name;o.age=  Age;o.job=job;o.sayname=function() {alert (this. name)}; return o;} var person1=createperson ("Chenjj", "Boss"); var person2=createperson ("WANGCC", "Boss");

Although the problem of creating multiple similar objects is resolved, there is no problem with object recognition (that is, how to know the type of an object)

/*
* Constructor Mode
*
*/
Constructor mode,
Like object and array these are native constructors, and we can also create custom constructors to instantiate, new, an instance. A constructor defined in this way is defined in the global object (which is a Windows object in the browser).

functionPerson (name,age,job) {//the first letter of the constructor is capitalized, This. name=name; This. age=Age ; This. job=job; This. sayname=function() {alert ( This. name);};}varperson3=NewPerson ("CHENJJ", "Boss");varperson4=NewPerson ("WANGCC", "Boss");//The two instances of Person3 and person4 all contain the constructor (constructor) attribute, which points to their constructor, the person.

Constructor problem: I Person3 and Person4 have a method is Sayname this method, the purpose of both methods are the same, say their name. But when I instantiate it, Person3 and Person4 are different function instances. It means that although we do the same thing, we are born differently. This results in different scope chains and identifier resolution.

The solution is to put the definition of the method outside the function as follows. So my Person3 and Person4 methods use the same function.

functionthis. name=name; this. age= age; this. job=job; this. sayname=sayname;} function Sayname () {alert (this. name);} var person3=New person ("CHENJJ", "Boss"); var person4=New person ("WANGCC", "Boss");

But then again, what if I have multiple methods? Am I going to define a lot of global functions? So, the prototype pattern is on.

/*
* prototype Mode
*
*/
//Each function we create has a prototype (prototype) attribute, which is a pointer to an object. This object contains properties and methods that can be shared by all instances of a particular type. Like the person constructor above, he also has a prototype attribute, which is the object that the prototype refers to as the prototype object of the person's instance. Is the prototype object of Person3 and Person4.

function Person () {}; Person.prototype.name= "Chenjian"; Person.prototype.age= "a"; Person.prototype.job= "a"; Person.prototype.sayName=function() {alert (this. name);}; var person5=New person (); var person6=New person (); // Person5 and Person6 all have the same properties and methods, Person5.sayname===person6.sayname;

When we do, Person5.name will be found in the Person5 instance, and some words will return the value of name.
If not, continue to find the prototype object.
We can rewrite the prototype object with the object literal.

function Person () {}; Person.prototype={name:"Chenjian", Age:"All", Job:"Boss", Sayname:  function() {alert (this. name);}}

But since we have rewritten the prototype object, the connection between the constructor and the prototype object is changed, and the constructor property of the prototype object no longer points to the original person constructor, but the object constructor. So rewrite the entire prototype object, the situation is not the same.
Prototype mode also has his problem, we know that with prototype mode, all instances can share the same data. But there must be something that belongs to you. It's impossible for you to change the name of the instance, and the names of the other instances will have to be the same as you.

/** * Combined use of constructors and prototype patterns*/functionPerson (name,age,job) { This. name=name; This. age=Age ; This. job=Job;} Person.prototype={constructor:person,sayname:function() {alert ( This. name);}}varperson7=NewPerson ("Chenjj", "$", "Boss");varperson8=NewPerson ("WANGCC", "Boss")

The properties of the instance are defined in the constructor, and you add and delete your properties without affecting the creation of my next instance. The shared properties and methods are defined in the prototype.

Creation of objects

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.