JavaScript Object-oriented understanding (i)

Source: Internet
Author: User

For the first time in the garden:

About JS Object-oriented understanding:

What is the factory approach? What is a constructor function? The prototype chain? A reference to an object?

1. What is the object?

In the JS contact more is the object, such as:

1 vararr = [];2 3Arr.number = 10;//variables below the object: Properties called Objects4 5 //alert (arr.number);6 //alert (arr.length);7 8Arr.a=function(){//functions below the object: Methods called Objects9Alert (123);Ten }; One  AARR.A ();//123

You can create objects by using the new Date () New String ().

2. What is the factory approach?

//Factory mode: encapsulation functionfunctionCreateperson (name) {//1. Raw Materials    varobj =NewObject (); //2. ProcessingObj.name =name; Obj.showname=function() {alert ( This. Name);    }; //3. Factory    returnobj; }varP1 = Createperson (' Xiao Ming ');p 1.showName ();

In Factory mode, you do not need a new function to create the object directly inside the function, by adding properties and methods to the new object, and then throwing it. The disadvantage is: not flexible enough.

3. What is a constructor function?

  

function Createperson (name) {        this. Name = name;       This function () {        this. name);}    ;    } var New Createperson (' Xiao Ming ');p 1.showName ();

By means of a new function outside the function, an object is instantiated as a constructor (the general function name follows uppercase). Inside the function this points to the instantiated object, not to the window. The properties and methods are mounted on the instantiated object. Cons: Each method is mounted on the newly created instantiation object, which creates a performance waste if many objects need to be instantiated.

Special attention:

P1.showname () = = P2.showname (); // false because, as above, the method above each instantiation object, in order to solve this problem, appeared in the prototype above Add method. 

4. References to Objects:

//Example 1:varA = [A.];varb =A; B.push (4); alert (b); //1,2,3,4; b is not assigned, just point the reference to a, modify B to change the value of a//Example 2:varA = [A.];varb =a;b= [1,2,3,4];alert (a); //- ----------the first B-reference points to a, but then B is assigned, re-creates the memory unit, assigns the value [1,2,3,4], the result does not affect a//Object Type: Replication is not only a copy of a value, but also a reference delivery//Example 3:varA = [A.];varb = [A.];alert (A= = b);//The object type must have the same value and reference as the

5. Prototypes:

function Createperson (name) {        this. Name= name;}
function () { this. name);}; var New Createperson (' Xiao Ming ');p 1.showName (); var p2 = new Createperson (' white '= = P2.showname ()) //True

//to array How to add Prototypes:  function() {  var result = 0;      for (var i = 0; i<. length; i++) {       this[i];             }   return  = [6 ]

JavaScript Object-oriented understanding (i)

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.