`
axgle
  • 浏览: 93506 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Javascript.new

阅读更多
In firefox, make Javascript.new like Ruby.new
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 ExtJS,you can use it like this:
Ext.Window.new({height:100, width:200}).show();
分享到:
评论
2 楼 axgle 2009-06-30  
just for fun.
1 楼 hackwaly 2009-06-29  
new是关键字和保留字,这么用符合ecma262标准么?

相关推荐

Global site tag (gtag.js) - Google Analytics