2013-03-27 65 views
0

我知道你可以設置一個新的對象的原型使用此功能(read mozzilla docu) ,但如果它是一個對象字面這樣沒有的Object.create還可以創建自己的屬性

return Object.create(this); 
中使用它也創造了自己的屬性

我也知道從字面克拉斯至極這種方法只複製實例方法

var subclass = function() { }; 
subclass.prototype = parent.prototype; 
klass.prototype = new subclass; 

主要是我感興趣的是的Object.create方法

EDIT

var Klass = { 
    init: function(){}, 

    prototype: { 
    init: function(){} 
    }, 

    create: function(){ 
    var object = Object.create(this); 
    console.log('object with class create'); 
    console.log(object); 
    console.log("object's parent is this"); 
    console.log(this); 
    object.parent = this; 
    object.init.apply(object, arguments); 
    console.log('returned object from create'); 
    console.log(object); 
    return object; 
    }, 

    inst: function(){ 
    var instance = Object.create(this.prototype); 
    console.log('de instance na object create'); 
    console.log(instance); 
    instance.parent = this; 
    instance.init.apply(instance, arguments); 
    console.log('arguments in inst'); 
    console.log(arguments); 
    return instance; 
    }, 

    proxy: function(func){ 
    var thisObject = this; 
    return(function(){ 
     return func.apply(thisObject, arguments); 
    }); 
    }, 

    include: function(obj){ 
    var included = obj.included || obj.setup; 
    for(var i in obj) 
     this.fn[i] = obj[i]; 
    if (included) included(this); 
    }, 

    extend: function(obj){ 
    var extended = obj.extended || obj.setup; 
    for(var i in obj) 
     this[i] = obj[i]; 
    if (extended) extended(this); 
    } 
}; 

Klass.fn = Klass.prototype; 
Klass.fn.proxy = Klass.proxy; 

感謝,理查德

+0

「*複製實例方法*」 - 什麼? – Bergi 2013-03-27 15:47:41

+0

所以'create'應該創建一個子類,'inst'應該創建該類的實例?爲什麼類需要一個'init'方法?你的問題是什麼,你想達到什麼目的? – Bergi 2013-03-27 19:07:59

回答

2

MDN Object.create

摘要

創建具有指定原型對象和屬性的新對象。

所以,讓我們來看看與該new關鍵字和一個與Object.create實例化對象的簡單例子;

function objectDotCreate() { 
    this.property = "is defined"; 
    this.createMe = function() { 
     return Object.create(this); 
    }; 
} 
var myTestObject = new objectDotCreate(); 
console.log(myTestObject, myTestObject.createMe()); 

JSBin

現在考慮看看控制檯輸出

Console Output

左:new右:Object.create

,你可以SE e都用它們的屬性創建一個對象的新實例。

Object.create只有

創建具有指定原型對象和屬性的新對象。

而且newMDN

[...]創建一個用戶定義的對象類型的實例或具有構造函數的內置對象類型之一。

所以實例,創建使用Object.create,能夠訪問到性能,因爲它們是由它的prototype陰影和其中使用new的人,都有自己的屬性,它的構造函數定義。

因此,不,它不會創建自己的屬性。(雖然你可以傳遞一個對象來直接定義對象屬性描述)

+0

好吧,我必須考慮這一點,因爲我正在做原型繼承。我有一個具有類屬性的模型,當我使用object.create(this)子類化它時,它將執行一個初始化函數,這是一個類函數..我推薦了我的問題。 – Richard 2013-03-27 16:28:33

+0

有兩個init函數,它執行class init – Richard 2013-03-27 16:35:14

+0

然後在你的示例firebug輸出中,右邊的站點獲取對象的屬性並將其放入原型中,然後該對象不應該擁有自己的屬性,但是在螢火蟲中,我仍然可以看到指向不同對象的類屬性/方法init函數的版本。然後它執行類上下文中的init函數。這是令人困惑的,因爲object.create(this)應該是一個沒有自己的/ class屬性/方法的新對象.. – Richard 2013-03-27 16:50:05

1

它也如果你讀了docs創建自己的屬性

,它說:沒有 - 除非你告訴它第二個參數就是這樣做的。它的基本用途是創建一個新的空的對象,其內部原型設置爲參數。第二個參數會像defineProperties那樣工作。

,如果它是一個對象字面這樣

return Object.create(this);

內使用我沒有看到任何物體這裏的文字,但只要你不使用第二個參數返回的對象沒有自己的屬性。

+0

謝謝,這會讓人困惑除非我粘貼全班 – Richard 2013-03-27 16:36:21

相關問題