The way JavaScript creates objects

Source: Internet
Author: User

<! DOCTYPE html>window.onload=function(){//1********************************************************************************************************** ****//var box=new Object ();//1: Basic method for creating a function//box.name= "Feige";//box.run=function () {//alert (this.name+ "Runnig ...")// }//    //Box.run ();////Disadvantage is that if you want to create a similar object to generate a lot of code   //2 Factory mode ***************************************************************************************************** *********//    //function Createobj (name,age) {//co-production mode creation Object//var obj=new Object ();//Obj.name=name;//Obj.age=age;//obj.run=function () {//alert (this.name+this.age+ "Running ...");//    }//    //return obj;//returns Object reference//    //    }//function CreateObj2 (name,age) {//co-production mode creation Object//var obj=new Object ();//Obj.name=name;//Obj.age=age;//obj.run=function () {//alert (this.name+this.age+ "Running ...");//    }//    //return obj;//returns Object reference//    //    }//var box1=createobj (' Feige ', 22);//Create first object//var box2=createobj (' cc ', 22);//Create a second object//Box1.run ();//Box2.run ();//var box3=createobj2 (' KKK ', 2);// ////ps: Although the factory model solves the problem of repeated instantiation, it is impossible to figure out which of them is an instance of the object, such as the following//alert (box1 instanceof Object);//true//alert (box3 instanceof Object);//still true// // //3 Construction Method Creation Object ************************************************************************************************** ************functionBox (name,age) {//Construction Method Creation Object (modified factory method) This. name=name; This. age=Age ; This. run=function() {alert ( This. name+ This. age);}}functionBox2 (name,age) {//constructing methods to create objects This. name=name; This. age=Age ; This. run=function() {alert ( This. name+ This. age);}}varbox1=NewBox (' CC ', 13);//instantiation ofvarBox2=NewBox2 (' cc ', 21);//instantiation of//alert (box1 instanceof Box);//alert (box2 instanceof Box);//differs from Factory mode to distinguish instance, Box2 is a reference to Box2 object//1. The constructor does not have a new object but the background will automatically var obj=new object ();//2.this is the equivalent of obj//3. The constructor does not need to return an object reference, he is automatically returned by the background//Specification//1. The constructor is also a function, but the first letter of the function name is capitalized//2. Must new constructor (), New Box ()//3. You must use the new operatorvaro=NewObject ();//object posing, you can take the entire constructor down to the OBox.call (O, "posing", 12) O.run ();}</script></body>

How JavaScript creates objects

Related Article

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.