Last active
November 3, 2016 17:41
-
-
Save ryanlid/8cee60aa7f67fc7005c39abe72adef99 to your computer and use it in GitHub Desktop.
函数调用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var add = function(a,b){ | |
return a + b; | |
}; | |
/* 方法调用模式 */ | |
// 创建myObject,它有一个 value 属性和一个 increment 方法。 | |
// increment方法接受一个可选的参数。如果参数不是数字,那么默认使用数字1。 | |
var myObject = { | |
value :0, | |
increment:function(inc){ | |
this.value +=typeof inc ==='number'? inc:1; | |
} | |
}; | |
myObject.increment(1); | |
document.writeln(myObject.value); // 1 | |
myObject.increment(2); | |
document.writeln(myObject.value); // 3 | |
/* 函数调用模式 */ | |
myObject.double = function(){ | |
var that = this; | |
var helper = function(){ | |
that.value = add(that.value,that.value); | |
}; | |
helper(); | |
}; | |
myObject.double(); | |
document.writeln(myObject.value); // 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment