发现在Pro JavaScript的技术书这个例子动态生成方法
function User(name, age) {
var year = (new Date()).getFullYear()-age;
this.getYearBorn = function(){
return year;
};
}
var user = new User("Bob", 44);
alert(user.getYearBorn()); //system date is 2010 ,alerts 1967
我基于上述逻辑改变系统日期2012
alert(user.getYearBorn()); //alerts 1968.
我写下面的代码
function Test(bornTime){
var ageInMillis = new Date().getTime()-bornTime.getTime();
this.age = function(){
console.log("age:"+ageInMillis);
}
}
var t = new Test(new Date());
t.age(); //output is zero
setTimeout(t.age,1000) // it prints 0
不应该第二次打电话给t.age打印1000, 我做错了什么?
没有进一步的背景,我不明白这是如何解决这个问题的。 – 2011-12-30 20:53:04