JS Object-oriented

Source: Internet
Author: User

I. Composition of the Object
/* Features of object-oriented programming Abstract: Seize the core issue encapsulation: Only through the object to access the method inheritance: from the existing objects to inherit the new object polymorphism: The different forms of multi-object 1. How objects are composed (behaviors, operations)-Functions: procedures, dynamic Properties--variables: state, static * */// The composition of the object var arr=[];//the variable below the object: The property called the object Arr.number=10;//alert (Arr.number);//alert (arr.length);// The function below the object: The method called the object Arr.test=function () {alert (123);} Arr.test ();
Two. Creation of the first object-oriented program
An empty object has been created, Var obj=new object ();//attribute obj.name= ' xiaoming ';//Method Obj.showname=function () {//alert (obj.name); alert (this.name) ;} Obj.showname ();
Three. Factory mode
/* Factory Method Object-oriented encapsulation functions are changed to resemble system objects. Initial capitalization New keyword extract this points to the newly created object when new                             goes to invoke a function: This is the image created by this function.    And the return value of the function is just this. (implicitly returned) the              function that the constructor uses to create the object, which is called                        a reference to the problem object that exists after the function called by the constructor new is wasting memory * *///var obj=new object ();// Obj.name= ' xiaoming ';//obj.showname=function () {//alert (this.name);//}//obj.showname ();//var obj1=new Object ();// Obj1.name= ' Xiao Qiang ';//obj1.showname=function () {//alert (this.name);//}//obj1.showname (); function Creatperson (name) {// 1. Raw material var obj=new Object ();//2. Processing obj.name=name;obj.showname=function () {alert (this.name);} 3. Exit return obj; var p1=creatperson (' xiaoming ');p 1.showName (), var p2=creatperson (' Xiao Qiang ');p 2.showName ();    Extract newfunction Creatperson (name) {//1. Raw material//var obj=new Object ();//2. Processing this.name=name;this.showname=function () { alert (this.name);} 3. Exit return obj; var p1=new creatperson (' xiaoming ');p 1.showName (), var p2=new creatperson (' Xiao Qiang ');p 2.showName ();                         
Four. Prototypes
/* Prototype: To overwrite a common method or property under an object, so that a common method or property exists in memory (improves performance) * *//*var A=[1,2,3];var B=[1,2,3];alert (a==b); False*//*var A=5;var B=a;b+=3;//alert (b);//Basic type: When assigning a value just copy alert (a); *//*var A=[1,2,3];var B=a;b.push (4);//alert (b) ;//[1,2,3,4]//Object Type: Assignment is not only a copy of the value, but also a reference for passing alert (a);//[1,2,3,4]*//*var A=[1,2,3];var B=a;b=[1,2,3,4];alert (b); [1,2,3,4]alert (a); [1,2,3]*///comparison var a=5;var b=5;//basic type: The same value can be alert (a==b); Truevar A1=[1,2,3];var b1=[1,2,3];//Object type: Both the value and the reference are the same. alert (A1==B1); Falsevar A2=[1,2,3];var b2=a2;//Object type: Both the value and the reference are the same. alert (A2==B2); True

Prototype

/* prototype-prototype concept to overwrite the common methods or properties under the object, To have a common method or property exist in memory (improve performance) Learn prototype analogy: The class in CSS uses the same properties and methods as the prototype to rewrite the Factory mode. Summary of the object-oriented method of constructing the constructor plus attributes, the prototype plus the function constructor () {this. property} constructor. prototype. Method =function () {} var object 1=new constructor (); Object 1. Method (); * *//*var Arr=[1,2,3,4];var arr1=[2,2,2,2,2,2,2];arr.sum=function () {var result=0;for (var i=0;i<this.length;i++) { Result+=this[i];} return result;} Arr1.sum=function () {var result=0;for (var i=0;i<this.length;i++) {result+=this[i];} return result;} Alert (Arr.sum ()); alert (Arr1.sum ()); */var Arr=[1,2,3,4,5];var arr1=[2,2,2,2,2,2,2]; Array.prototype.sum=function () {var result=0;for (var i=0;i<this.length;i++) {result+=this[i];} return result;} Alert (Arr.sum ()); alert (Arr1.sum ()); array.prototype.number=20;//Priority High Arr.number=10;alert (Arr.number); 10

  

  

  

  

  

JS Object-oriented

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.