Js_ Object-oriented design and behavior delegation design pattern

Source: Internet
Author: User

Recently changed a new job, there are some problems in the interview process did not give the answer, for their technical short board and promote the urgent.

JavaScript is the object of Everything (key: A value of a data), for the moment not to discuss the right and wrong sentence, you can imagine the importance of the object in JavaScript, and object-oriented design pattern is to go to the advanced development of the first part of the necessary skills.

Three ways to create objects

creating objects by literal volume

 1  //  1. Create an empty object  2  var  obj = {};  3  4  //  2.obj object Add property  5  obj.name = ' obj '  6  // 3. Obj Object Add method  7  obj.tostring = function   (str) { 8   return   str;  9 } 

New + Constructors Create objects

1 //1. Create a base constructor2 functionBase () {3Console.log (888)4 }5 6 //2. Create an Obj object using New+base ()7 varobj =NewBase ();8 /*New specifically did the following three things9 var obj = {};Ten obj.__proto__ = Base.prototype; One Base.call (obj); A return obj; - in the first line, we created an empty object, obj. - In the second line, we point the __proto__ member of this empty object to the base function object prototype member object the In the third line, we replace the this pointer of the base function object with obj and then call the base function, so we give obj - an ID member variable is assigned an object, and the value of this member variable is "base", about the use of the call function.  - The fourth line returns the new object.  - * */ +  - //3. Add a prototype method to the Obj object, note that this is not a obj.prototype, but a base.prototype.  +Base.prototype.toString =function(){ A     return  This. ID; at } -  -Console.log (obj);

Behavior Delegate mode Creation object (Object.create ())

1 //1. Create a Behavior object foo2Foo = {3Init:function(WHO) {4Console.log (9999)5     },6Indentify:function() {7Console.log (8888)8     }9 };Ten //2.bar Behavior Object inherits Foo behavior object (the Foo behavior object is delegated to bar behavior object) OneBar =object.create (foo); A  - //3.b1 and B2 Inherit bar behavior objects (delegate bar behavior objects to B1 and B2 behavior objects) - varB1 =object.create (bar); the varB2 =object.create (bar); -  -  - //The behavior delegates above are delegated in the form of a prototype chain.  + Console.log (foo); - Console.log (bar); +Console.log (B1);

Js_ Object-oriented design and behavior delegation design pattern

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.