標籤:cPage blog io os ar for sp art 問題
#1預設原型繼承
function inherint(C,P){
C.prototype = new P();
}
function Parent(name){
this.name =name||"Adam";
}
Parent.prototype.say = function(){
return this.name;
}
function Child(name){
// this.name = name;
}
inherint(Child,Parent);
var kid = new Child("lishuang");
kid.say();//"Adam"
this---
Child {name: "Adam", say: function}
缺點:
1繼承了兩個對象上的屬性即添加到this上的和添加到原型上的,多數時不許這些自身屬性
2每次需要一個子類都要調用繼承函數
3原型上的引用屬性會共用
#2借用建構函式
// 借用建構函式
function Article(){
this.tags =[‘js‘,‘css‘];
}
var article = new Article();
function BlogPost(){}
BlogPost.prototype = article;
var blog = new BlogPost();
// 注意以上不在需要new Article() 因為已經有了可用執行個體
function StaticPage(){
Article.call(this);
}
var page = new StaticPage();
console.log(article.hasOwnProperty(‘tags‘));
console.log(blog.hasOwnProperty(‘tags‘)); // 擷取的是原型上引用
console.log(page.hasOwnProperty(‘tags‘)); //會建立一個副本而不是引用
true
false
true
//缺點:
無法從原型中繼承任何東西,且原型也僅是添加可重用方法以及屬性的位置不會為每個實力重新建立原型
、、優點
可以獲得父物件自身成員的真實副本不存在子物件意外覆蓋父物件屬性的風險
#3 借用和設定原型
function Parent(name){
this.name =name||"Adam";
}
Parent.prototype.say = function(){
return this.name;
}
function Child(name){
Parent.apply(this,arguments);
}
Child.prototype = new Parent();
var kid = new Child("lishuang");
kid.name;
"lishuang"
kid.say()
"lishuang"
delete kid.name
true
kid.say()
"Adam"
#4 共用原型鏈------子孫對象修改原型會改變父物件的原型
function inherint(C,P){
C.prototype=P.prototype;
}
#5臨時建構函式
function inherit(C,P){
var F = function(){};
F.prototype = P.prototype;
C.prototype = new F();
}
儲存超類
function inherit(C,P){
var F = function(){};
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
}
重設建構函式
var inherit = (function(){
var F = function(){};
return function(C,P){
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
C.prototype.constructor = C;
}
}())
5原型繼承
function object(o){
functionF(){};
F.prototype= o;
returnnew F();
}
function Person(){
this.name= "Adam";
}
Person.prototype.getName=function(){
returnthis.name;
}
var para = newPerson();
var kid = object(para);//改進var kid = object(Person.prototype);
kid.getName();
ECMAscript5 裡用Object.create()實現
Object(parent,{})第二個參數可選
YUI3 Y.Object()方法
YUI().use(‘*‘,function(Y){
var child = Y.Object(parent);
})
6// 通過複製來實現繼承
// 淺複製當屬性是對象數組時會通過飲用傳遞存在修改父元素的風險
functionextend(parent,child){
vari;
child= child ||{};
for(iin parent){
child[i]= parent[i];
}
returnchild;
}
// 深賦值
functionextendDeep(parent,child){
var i,
tostr=Object.prototype.toString,
astr = "[object Array]";
child=child||{};
for(i in parent){
if(parent.hasOwnProperty(i)){
if(typeofparent[i] ==="object"){
child[i]= (tostr.call(parent[i]))===astr?[]:{};
extendDeep(parent[i],child[i]);
}else{
child[i]= parent[i];
}
}
}
return child;
}
函數綁定解決、回呼函數與函數運算式的this指向全域對象問題
var one = {
name :"obj",
say:function(greet){return greet+"," + this.name;}
}
one.say(‘hi‘);
//"hi,obj"
varsay = one.say; say(‘hi‘);
//"hi,"
function bind(o,m){
return function(){
returnm.apply(o,[].slice.call(arguments));//閉包是的函數返回後仍可訪問o/m
};
}
ES5 中Function.prototype.bind的實現
if(typeofFunction.prototype.bind==="undefined"){
Function.prototype.bind= function(thisArg){
varfn = this,
slice = Array.prototype.slice,
arg= slice.call(arguments,1);
return function(){
returnfn.apply(thisArg,arg.concat(slice.call(arguments)));
}
}
}
js繼承方式