js面向原型話語言總結
一、數組的尋找和刪除
function WarrperArray(array){
this.array = array;
}
//返回下標
WarrperArray.prototype.existValue = function(val){
for(var j = 0;j if(this.array[j]==val) return j;
}
return -1;
}
//刪除指定值得元素
WarrperArray.prototype.removeValue = function(val){
var index = this.array.indexOf(val);
if(index>-1){
this.array.splice(index,1);
return this.array;
}
}
二、原型建構函式的總結
/**
//js原型物件建構函數,浪費記憶體、將不變的屬性和函數定義在原型上Prototype模式
function Cat(name,color){
this.name=name;
this.color = color;
}
//將公用的方法和屬性定義在原型上,節約記憶體的開銷
Cat.prototype.type="TYPE_CAT"
Cat.prototype.eat = function(){
alert("eat mouse");
}
*/
三、建構函式的繼承 function Animal(){
this.species = "動物";
}
/**
//建構函式
function Cat(name,color){
this.name = name;
this.color = color;
}*/
//繼承方法一建構函式綁定(子類擁有父類的屬性和方法)建構函式是子類的
function Cat(name,color){
Animal.apply(this,arguments);
this.name =name;
this.color = color;
}
//繼承方法二 prototype模式
/**
Cat.prototype = new Animal();//將父類賦值給子類原型
Cat.prototype.constructor = Cat;//將構造方法設定為子類
*/
/**利用Null 物件作為中介
var F = function(){};
F.prototype = Animal.prototype;//將父類的原型賦值為空白對象
Cat.prototype = new F();將中介執行個體賦值給子物件
Cat.prototype.constructor = Cat;
*/
四、利用Null 物件繼承封裝成一個方法 function extend(Child, Parent) {
var F = function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.uber = Parent.prototype;
}
/**使用如下
extend(Cat,Animal);
var cat1 = new Cat("大毛","黃色");
alert(cat1.species); // 動物
*/
/**五、建構函式 拷貝繼承*/function extend2(Child,parent){
var p = parent.prototype;
var c = Child.prototype;
for(var i in p){
c[i] = p[i];
}
c.uber = p;//留出父類的屬性
}
/**六、非建構函式的繼承*/var Chinese ={nation:'中國'};
var Doctor = {carteer:'醫生'};
//中國醫生 把子物件的原型賦值給父物件讓其串連在一起
function object(o){
function F(){};
F.prototype = o;
return new F();
}
/**
使用如下 在父物件基礎上產生子物件
var Doctor = object(Chinese);
然後加上子物件本身的屬性
Doctor.career = '醫生';
這時子物件已經繼承了父物件屬性
alert(Doctor.nation);
*/
/**七、淺複製copy*/function extendCopy(p){
var c = {};
for(var i in p ){
c[i] = p[i];
}
c.uber = p;
}
/**使用如下*/
var Doctor = extendCopy(Chinese);
Doctor.career ='醫生';
alert(Doctor.nation);
//此複製為淺copy 若屬性存在數組和引用,則修改子類的屬性,父類的屬性也發生變化;
//舉例
Chinese.birthPlaces=['北京','上海','香港'];
//通過extendCopy()函數Doctor繼承Chinese
var Doctor = extendCopy(Chinese);
//然後為Doctor的出生地添加一個城市:
Doctor.birthPlaces.push("河南");
//查看父物件Chinese,發現出生地被修改
alert(Chinese.birthPlaces);//'北京','上海','香港',河南
alert(Doctor.birthPlaces);//同上
//所以這種淺拷貝不適用與繼承
//深拷貝
function deepCopy(p,c){
var c = c||{};
for(var i in p){
if(typeof(i) =='object'){
c[i] =(p[i].constructor==Array)?[]:{};
deepCopy(p[i],c[i]);//遞迴寫法
}else{
c[i] = p[i];
}
}
}
//使用如下:
var Doctor = deepCopy(Chinese);
//給父物件加一個屬性,出生地數組
Chinese.birthPlaces=['北京','上海','香港'];
//然後在子物件上修改屬性
Doctor.birthPlaces.push("河南");
//查看這兩個對象的屬性值
alert(Doctor.birthPlaces);//'北京','上海','香港',河南
alert(Chinese.birthPlaces);//'北京','上海','香港'
//目前,jQuery庫使用的就是這種繼承方法。
//引用資料類型也可以用建構函式返回
//舉例如下:
function clone(obj){
function F(){};
F.prototype = obj;
return new F();
}
var Chinese ={
nation:'中國',
createBirthPlaces:function(){
return ['北京','上海','香港'];
}
}
var Doctor = clone(Chinese);
Doctor.career ='醫生';
Doctor.birthPlaces=Chinese.createBirthPlaces();
alert(Doctor.birthPlaces.length);//3
Doctor.birthPlaces.push("大連");
alert(Doctor.birthPlaces.length);//4
alert(Chinese.birthPlaces.length);//3