Object-oriented - prototype
1. Object Oriented (OOP)
1.内置对象
//构造函数function Array(){} //由于window内置了所以不用写直接实例化就好了
//Array是内置对象
var arr1 = new Array(1,2,3);
var arr2 = new Array(4,5,6);
alert(arr1.constructor); //constructor可以获取实例对象的构造函数
alert(arr1.push == arr2.push) //true //这里可以说明他们共用一个方法
//构造函数的特点:首字母大写,new关键字就行调用,this指向实例对象
2.构造函数实现面向对象
function Abc(name){
this.name = name;
this.showname = function(){
return this.name
}
}
var a1 = new Abc('zhangsan');
var a2 = new Abc('wangwu');
alert(a1.showname()) //输出zhangsan
alert(a1.showname == a2.showname) //false 此时不在是同一个方法 为什么呢?
The constructor of the drawbacks: for instance for each method, we must re-create once, properties and methods inside constructors are private.
Summary: The method object instance constructor object systems are built the same method
We own instance of an object's constructor method is not the same method
True object-oriented: Hybrid development (+ constructor prototype)
The constructor for: Properties and Methods of the interior is different
Built-in objects: internal property methods are the same
//构造函数
function Abc(name){ //构造函数内部的属性和方法都是不同的,私有的
this.name = name;
this.showname = function(){
return this.name
}
}
prototype
Prototype: Each function has a convention prototype (prototype property of the object), is placed inside the public properties and methods, this prototype is still inside the instance object points
function Abc(name){
this.name = name;
this.showname = function(){
return this.name
}
}
Abc.prototype.showname = function(){
return this.name;
}
var a1 = new Abc('zhangsan');
var a2 = new Abc('wangwu');
//就在这个时候突然发现 //alert(a1.showname == a2.showname) //true
总结:公有的方法都放在原型下面 属性放在构造函数下面