//程式碼1
var person = {
firstname = 'John',
lastname = 'Doe',
getFullFunction: function (){
var fullname = this.firstname + ' ' + this.lastname;
return fullname;
}
}
//程式碼2
var logName = function(lang1, lang2){
console.log('Logged: ' + this.getFullName())
}
//執行失敗
logName();
//程式碼3
var logPersonName = logName.bind(person)
logPersonName();
//程式碼4
var logName = function(lang1, lang2){
console.log('Logged: ' + this.getFullName())
}.bind(person)
logName();
//程式碼5
var logName = function(lang1, lang2){
console.log('Logged: ' + this.getFullName())
}
logName.call(person, 'en', 'es');
//程式碼6
logName.apply(person, ['en', 'es']);
//程式碼7
(function(lang1, lang2){
console.log('Logged: ' + this.getFullName())
}).apply(person, ['en', 'es']);
//function borrowing
var person2 = {
firstname = 'Jane',
lastname = 'Doe'
}
console.log(person.getFullName.apply(person2));
//function currying
function multiply(a, b) {
return a * b;
}
var multiplyByTwo = multiply(this, 2);
//印出8
console.log(multiplyByTwo(4));