我有这个简单的类:
function Page(u,o) {
this.o = o;
this.gen(u);
}
Page.prototype = {
gen:function(u) {
if(u ==='index.php') new test(this.o);
}
}
new Page('index.php',{data:"just for test"});
有没有办法使用 Object.create(this); 而不是 new test(this.o); 这样测试类就可以访问 this.o?,而不是每次都生成新的测试实例?如您所见,我不使用 var x = new ...因为我不需要它。测试类只需使用 this.o 在div 元素中附加一些数据,不返回任何内容。谢谢
不要使用类,只需将测试设置为普通函数(如果尚未)并删除"new"。就此而言,由于看起来您也不对 Page
类执行任何操作,因此也将其设置为直接函数:
function page(u, o) {
if (u === 'index.php') {
test(o);
}
}