var peopleFactory = function(name, age, height) {
var temp = {};
this.name = name;
this.age = age;
this.height = height;
temp.printPerson = function() {
console.log(this.name + '' + this.age + '' + this.height);
document.write(this.name + '' + this.age + '' + this.height);
};
return temp;
};
var person1 = peopleFactory('tanmay', 27, 5.11);
var person2 = peopleFactory('chinmay', 37, 5.12);
person1.printPerson();
person2.printPerson();
答案 0 :(得分:0)
您不应该在工厂中使用this
,因为它是对全局对象的引用(除非您想使用new
关键字致电您的工厂。但是,它不会是工厂了。
相反,您可能正在使用另一个本地对象来存储对象的私有数据。通过这样做,您的printPerson()函数成为一个闭包,可以访问该本地对象内的数据,并且一旦调用它就能够打印它。
var peopleFactory = function(name, age, height) {
var temp = {}, instance = {};
temp.name = name;
temp.age = age;
temp.height = height;
instance.printPerson = function() {
console.log(temp.name + ' ' + temp.age + ' ' + temp.height);
document.write('<br/>' + temp.name + ' ' + temp.age + ' ' + temp.height);
};
return instance;
};
var person1 = peopleFactory('tanmay', 27, 5.11);
var person2 = peopleFactory('chinmay', 37, 5.12);
person1.printPerson();
person2.printPerson();
答案 1 :(得分:0)
不确定但是你走了。只要把它变成一个类。
import utest._
...
val name = implicitly[utest.framework.TestPath].value