In firefox, make Javascript.new like Ruby.new
with [url=http://extjs.com/]ExtJS[/url],you can use it like this:
Object.prototype['new']=function(){
var params=[];
var argc=arguments.length;
for(var i=0;i<argc;i++){
params.push('arguments['+i+']');
}
var code='return new this('+params.join(',')+');';
return new Function(code).apply(this,arguments);
}
Number.new(100);//100
String.new("text");//"text"
Array.new(1,2,3);//[1,2,3]
function Person(name){
this.name=name;
this.toString=function(){return this.name;}
}
tom=Person.new('Tom');
joy=Person.new('Joy');
tom+" and "+joy;with [url=http://extjs.com/]ExtJS[/url],you can use it like this:
Ext.Window.new({height:100, width:200}).show();
本文介绍了一种在JavaScript中自定义new操作的方法,通过这种方式可以更灵活地创建对象实例。文章提供了具体的实现代码,并展示了如何使用自定义new操作来创建不同类型的对象。
881

被折叠的 条评论
为什么被折叠?



