调用时这么写, :
var comm = new xtComm.Coord("Add");
xtComm.Coord.Calc(1, 43);下面的代码怎样编写,可以满足 像上面那段代码的调用? 【要求:模块化写法(符合AMD规范)】
var xtComm = (function () {
var Coord = (function () {
var __type = "";
var ctor = function (type) {
this.__type = type;
}
var proto = ctor.prototype;
proto.add = function (n1, n2) {
var result = 0;
if(__type == "add"){
result = n1 + n2;
}
return result;
}; return proto;
})(); xtComm.Coord = Coord;
return xtComm;
})();js amd
var comm = new xtComm.Coord("Add");
xtComm.Coord.Calc(1, 43);下面的代码怎样编写,可以满足 像上面那段代码的调用? 【要求:模块化写法(符合AMD规范)】
var xtComm = (function () {
var Coord = (function () {
var __type = "";
var ctor = function (type) {
this.__type = type;
}
var proto = ctor.prototype;
proto.add = function (n1, n2) {
var result = 0;
if(__type == "add"){
result = n1 + n2;
}
return result;
}; return proto;
})(); xtComm.Coord = Coord;
return xtComm;
})();js amd
xtComm.Coord.Calc(1, 43);这里写错了吧,要调用实例的方法。。你那样写就是静态的了
comm.Calc(1, 43);
var xtComm = (function () { var Coord = (function () {
var __type = "";
var ctor = function (type) {
/*this.*/__type = type; //不能用this,要不Calc也要通过this来调用
}
var proto = ctor.prototype; proto.Calc = function (n1, n2) {
var result = 0;
if (__type == "add") {
result = n1 + n2;
}
return result;
}; return ctor;
})();
return { Coord: Coord };
/* xtComm.Coord = Coord;//不能这样引用xtComm,会报错,xtComm都还没初始化
return xtComm;*/
})(); var comm = new xtComm.Coord("Add");
alert(comm.Calc(1, 43))