关于将javascript自定义函数封装为类的写法,求救

iverylovecsharp 2006-12-04 11:34:23
我自己写一些javascript函数,想把他封装成这个效果,怎么办

abc.math.add(a,b)
abc.math.divide(a,b)
...
abc.date.format()
abc.date.add()
...

在用户调用时
只需
var test = new abc();
test.date.format()

或者

var test = new abc.date();
test.format()

就像java中的将一些函数以功能进行分类打包一样,怎么做
...全文
367 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
foyuan 2006-12-05
  • 打赏
  • 举报
回复
http://www.cnblogs.com/robinhood/archive/2006/09/26/515412.html
rob123 2006-12-04
  • 打赏
  • 举报
回复
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}

var test = Class.create();
test.prototype = {
initialize:function(a1,a2){
var ars1 = a1;
var ars2 = a2;
var a = Class.create();
a.prototype = {
initialize:function(a){this.text = a;},
show:function(){alert(this.text);}
};
this.a1 = new a(ars1);
this.a2 = new a(ars2);
},
add:function(){alert(this.a1.text + this.a2.text);}
}
var test = new test(1,2);
test.a1.show();
test.a2.show();
test.add();
iverylovecsharp 2006-12-04
  • 打赏
  • 举报
回复
up
mingxuan3000 2006-12-04
  • 打赏
  • 举报
回复
http://bbs.51js.com/viewthread.php?tid=59897

看看这个
bai11 2006-12-04
  • 打赏
  • 举报
回复
function s(){
this.s=1
this.getxx = function(){
return this.s
}
}

俺习惯这么写,哦哦
bai11 2006-12-04
  • 打赏
  • 举报
回复
一个function 套若干function
cjd2006 2006-12-04
  • 打赏
  • 举报
回复
同意楼上的
li1229363 2006-12-04
  • 打赏
  • 举报
回复
使用类,就可以达到楼主的希望了。不光光是JavaScript,php、asp、asp.net这些脚本语言都支持这种用法。当然,Java是绝对支持的。不用管用C写的CGI了,C不支持类
wuxinlangman 2006-12-04
  • 打赏
  • 举报
回复
你去看看prototype.js能收益很多的

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧