论坛首页 Web前端技术论坛

Javascript.new

浏览 1798 次
锁定老帖子 主题:Javascript.new
精华帖 (0) :: 良好帖 (0) :: 新手帖 (7) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-06-25   最后修改:2009-06-25
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();
   发表时间:2009-06-29  
new是关键字和保留字,这么用符合ecma262标准么?
0 请登录后投票
   发表时间:2009-06-30  
just for fun.
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics