window.number = 1
var obj = {
'number':4,
'dbl':(function(){
this.number *=2
return function(){
this.number *=2
}
})()
}
var dbl = obj.dbl
dbl()
obj.dbl()
console.log(window.number + obj.number)
执行函数dbl 时,this指向了window, 所以window.number = 122 = 4,执行 函数obj.dbl()时,this指向了obj,所以 obj.number = 8, 最终结果为12
function C1(name){if(name) this.name = name}
function C2(name){this.name=name}
function C3(name){this.name = name || 'John';}
C1.prototype.name = 'Tom';
C2.prototype.name = 'Tom';
C3.prototype.name = 'Tom';
console.log((new C1()).name + (new C2()).name + (new C3()).name)
(new C1()).name
没有传值,在C1.prototype上取值,所以是’Tom’
(new C1()).name
没有传值,默认是undifined,所以name 值 为 undifined
(new C1()).name
没有传值,默认是 ‘John’,所以name 值 为 ‘John’
//请定义这样一个函数
function repeat (func, times, wait) {
}
//这个函数能返回一个新函数,比如这样用
var repeatedFun = repeat(console.log, 3, 2000)
//调用这个 repeatedFun ("hellworld")
//会alert十次 helloworld, 每次间隔5秒
function repeat (func, times, wait) {
return function(){
var handle,i=0,arg=arguments
handle = setInterval(function(){
if(++i === times){
clearInterval(handle)
}
return func.apply(null,arg)
},wait)
}
}
var repeatedFun = repeat(console.log, 3, 2000)
repeatedFun ("hellworld")
网友评论