2015-03-19 56 views
0

在DOJO的declare(className,superclass,props)中有什麼用途classNameDOJO聲明中可選的className參數的用法是什麼?

在下面的示例中,我嘗試在使用遺產時使用className。

http://jsfiddle.net/mosnpm27/

當傳遞className我收到一個錯誤。

declare(className,superclass,props); 

className Optional 
The optional name of the constructor (loosely, a "class") stored in the "declaredClass" property in the created prototype. It will be used as a global name for a created constructor. 

require(["dojo/_base/declare"], function(declare) { 
    var Mammal = declare('Mammal',null, { 
     constructor: function(name) { 
      this.name = name; 
     }, 
     sayName: function() { 
      console.log(this.name); 
     } 
    }); 
    var Dog = declare('Dog', Mammal, { 
     makeNoise: function() { 
      console.log("Waf waf"); 
     } 
    }); 

    var myDog = new Dog("Pluto"); 
    myDog.sayName(); 
    myDog.makeNoise(); 

    console.log("Dog: " + myDog.isInstanceOf(Dog)); 
    console.log("Mammal: " + myDog.isInstanceOf(Mammal)); 
}); 
+0

有用的文章:http://dojotoolkit.org/documentation/tutorials/1.9/declare/ – GibboK 2015-03-20 07:40:18

回答

2

我不知道你收到了什麼錯誤,但className參數基本上是有隻遺留原因。聲明的類被放置在一個具有該名稱的全局變量中,但是當您使用AMD時,您並不需要這樣做。

例如,如果你做了這一點:

var Dog = declare('MyLibrary.Doggie', Mammal, { 
    makeNoise: function() { 
     loglog("Waf waf"); //console.log("Waf waf"); 
    } 
}); 

一個名爲MyLibrary全局對象會被創建,包含名爲Doggie成員。所以後來,你可以寫:

var myDog = new MyLibrary.Doggie("Pluto"); // instead of Dog("Pluto"); 
myDog.sayName(); 
myDog.makeNoise(); 

我不認爲有任何理由在所有時下,雖然做到這一點,所以你應該忽略className參數。

var Mammal = declare(null, { .... }); 
var Dog = declare(Mammal, { .... }); 
+0

是,其實我已刪除的className,它工作正常。感謝您的解釋 – GibboK 2015-03-19 12:00:14

相關問題