construtor接受一個key word argument和 一個srcNode。
construtor的調用結束之后kwArgs的屬性會被合并到新生成的對象中。如果kwArgs中有和prototype中同名的屬性,后果是這樣:
dojo.declare("some.Sample", [some.widget],
{
text: "",
constructor: function(kwArgs) {
this.text = kwArgs['text'].toUpperCase();
},
postCreate: function() {
console.info(this.text); // 你以為應該是大寫對吧
}
});
new some.Sample( {text: "hello, world"} );
結果:
hello, world //其實還是小寫, 因為kwArgs中的text被合并進當前對象的狀態,已經是upper case的對象的text又被kwArgs中的小寫覆蓋了。
關于constructor的kwArgs是如何確定的:
在new XXX()的情況下,是在代碼中確定的,像上面的例子。這是比較常見的情況。
如果對象是由標簽方式生成的(就是使用dojo.parser),kwArgs是由parser根據prototype的屬性讀取標簽的attribute生成的,如果標簽上有不在prototype中的屬性,這個屬性不會被包含在kwArgs的屬性中。
dojo.declare("some.Sample", [some.widget],
{
// text: "", 被去掉了
constructor: function(kwArgs) {
console.info(kwArgs['text']);
}
});
使用new XXX()的方式:
new some.Sample({text: "hello, world"});
結果:
hello, world
使用標簽方式:
...
<div dojoType="some.Sample" text="hello, world" />;
...
結果:
undefined // 因為text不是prototype的一個屬性
posted on 2007-11-02 10:17
zozilla 閱讀(627)
評論(0) 編輯 收藏