我们在dojo同级目录中创建一个文件夹itfanr,itfanr中新建一个文件person.js : define(["dojo/_base/declare"],function(declare){returndeclare("itfanr.person",null,{name:"zhangsan",constructor:function(name){this.name=name
我们在dojo同级目录中创建一个文件夹itfanr,itfanr中新建一个文件person.js :
define([ "dojo/_base/declare"], function (declare) { return declare("itfanr.person", null, { name: "zhangsan", constructor: function (name) { this.name = name; }, say: function () { return this.name; } }); }) ;
在index.html中使用这个类:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello Dojo!</title> </head> <body> <script src="dojo/1.10.0/dojo/dojo.js" data-dojo-config="async: true"></script> <script> require([ "itfanr/person"], function ( person) {//不知道为什么,必须要加入数组的[],即使只有一个外部类 var p = new person("lisi"); console.log(p.say()); }); </script> </body> </html>
如果我们需要创建一个无状态的工具类,那么我们可以只用define即可:
define([ "dojo/dom"], function(dom) { return { setRed: function(id){ dom.byId(id).style.color = "red"; } }; });
参考:http://blog.csdn.net/eengel/article/details/13021687