The prototype of js and the use of Construction method
The difference between classes and objects
Class: mold
Object: product (finished product)
For example:
Var arr1=new Array (12, 10, 11, 112, 111, 52)
Array: class
Arr1: object
Can only arr1.push (), not Array.push ()
Use constructor to add attributes, prototype add method
Function createPerson (name,qq) / / constructor
{
/ / after the new is created, the system automatically declares:
/ / var this=new Object ()
This.name=name
This.qq=qq
/ / return obj
/ / the system will automatically return:
/ / return this
}
Var obj=new createPerson ('Zhan Shaonan', '265404540')
Var obj2=new createPerson ('Esteban', '112233')
CreatePerson.prototype.showName=function () / / prototype
{
Alert ('my name is:'+ this.name)
}
CreatePerson.prototype.showqq=function ()
{
Alert ('my QQ is:'+ this.qq)
}
Obj.showName ()
Obj.showqq ()
Obj2.showName ()
Obj2.showqq ()