The creation object of the object-oriented programming of "The Day wins the golden creation"

Source: Internet
Author: User
Tags hasownproperty

Object definition: A collection of unordered attributes, which can be a base value, an object, or a function.
Each object is created from an application type, which can be built-in (for example, Object Array Math) or user-defined.

Object creation based on object

All objects are inherited from object, so we can start by creating objects from object.

//Create an object from the New keyword var New ' Yuhualinfeng '  - ' web Developer ' //Create objects from object literals var ' Yuhualinfeng '  - ' web Developer ';

Object-based objects are created in two forms, one using the new keyword, and the other using object literals.
The disadvantage of creating objects in this way is that when you create multiple objects of the same type, there is a lot of duplicated code, and if I want three person objects, I need to write three identical code, in order to solve this problem, we introduce the factory schema to create the object.

Creating objects using Factory mode

Factory mode is a widely recognized design pattern in the Software factory domain, which abstracts the process of creating concrete objects.

  function  createperson   ( Name,age,job)   {var  obj = new  object  (); obj.name = Name;obj.age = Age;obj.job = Job; return  obj;} var  Person1 = Createperson ( ' Yuhualingfeng ' , 30 ,  Web Developer ' ); var  Person2 = Createperson ( Obama , 45 ,  ' president ' ); 

We created two character objects, and if we created the object based on object, then the code inside the Createperson would be coded repeatedly.
But there is still a problem with objects created with this pattern: there is no way to know the type name of the object that was created. The possible solution to this problem is to use the constructor to create the object.

Creating an object using the constructor pattern
  function  person   (name, Age,job)   {this . Name = name; this . Age = age; this . Job = job; this . Sayname =  function   ()      {alert (this . Name); }} var  person1 = new  person ( ' Yuhualingfeng ' , 30 ,  Web Developer ' ); var  person2 = new  person ( ' Obama ' , ,  ' president ' ); 

Here we create a reference type named person, and then we instantiate this reference type with the New keyword, which can be refined into the following four procedures:

    1. Create a new object

    2. Assigns the scope of the constructor to the new function (so this will point to the new object)

    3. Executing the code in the constructor (assigning a value to the this object, equivalent to assigning a value to the new object)

    4. return new Object

We can use instanceof to detect whether the object type of Person1,person2 is person.

instanceof //true instanceof //true instanceof Object //true Because the person inherits from object, this is true here.

Note: A careful friend should notice that the first letter of the constructor here is capitalized, followed by a specification, the first letter of the normal function, the first letter of the normal function lowercase.

Constructors also have their own shortcomings, you can see that the person contains a Sayname function (method), the function is also an object (functional function of the instance), so each instantiation of a person, will produce a sayname method, that is, an object,
With the creation of the person instance how many, the resulting object also increased correspondingly, eventually lead to more memory, then we can find a better solution, is certain.

Creating objects using prototype mode

Each function we create has a prototype (prototype) attribute, which is a pointer to an object and the purpose of this particular object is to include properties and methods that can be shared by all instances of a particular type . This means that the prototype object does not take up two of the instances
More memory. Each prototype object has a constructor property by default, so the name Incredibles, which points to the constructor. The following shows the creation of objects from a prototype object.

  function  person   ()   {}person.prototype.name =  "Yuhualingfeng" ; Person.prototype.age = 30 ; Person.prototype.job = ; Person.prototype.sayname=  function   ()   {alert (this . name);}; var  person1 = new  person ();p Erson.sayname ( ); //yuhualingfeng  var  person2 = new  person (); 

Here Person.prototype.constructor points to person. You can also create objects by assigning values directly to the prototype object, as follows.

Person.prototype={constructor:person,name:"Yuhualingfeng", Age:$, Job:"web Developer" };

The reason for adding the constructor property here is that assigning a value directly to the prototype object will point the prototype object's pointer to another object, and the previously default value will not be accessible.

By the way, we introduce two methods and in keywords related to prototype objects:

    1. isPrototypeOf: Determines whether an object is a prototype of an instance.

True
    1. hasOwnProperty: Detects whether a property exists in an instance or in a prototype object.

Alert (Person1.hasownproperty (' name '//false, because the attribute exists in the prototype.
    1. In operator: In operator there are two ways to use, one is used alone, and for use, a separate use of the role is to determine whether a property is accessed in an instance (whether in the instance itself or in the prototype object), For-in is used in enumerations (loops).

//Determine if a property exists in the prototype  functionhasprototypeproperty(object,name)  return  in object);}

The disadvantage of prototype schema creation objects is that the prototype object of an instance is shared, and when you modify the properties of an instance, if the value of the property is a method or base type, there is no effect, and when the property is a reference type, it affects the property values of the other instance.
Combining the constructor pattern and the prototype schema to create the object, we combine their advantages with refine, we combine the constructor pattern with the prototype pattern.

Combining constructor mode prototype mode with

The disadvantage of creating objects through constructors is that each method is re-created on the instance, causing unnecessary memory consumption, and the disadvantage of creating objects from the prototype is that the properties of the instance reference type values affect each other. Considering that, we can put the storage merit attribute in the constructor and put the method in the prototype object. This pattern is the most widely used form of creating objects, which can be said to be the default mode for creating objects .

function  Person (name,age,job)   This  This this. Job = job;} Person.prototype = {Constuctor:person, sayname:function(){     alert ( this var New Person (' Yuhualingfeng ',',' web Developer ');p erson.sayname ();

These are some of the patterns of creating objects, and you can weigh the pros and cons of them with the usefulness of your own creation, and then choose the pattern that is right for you to create the object.

"Days to win" object-oriented program design object creation

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.