var myName = '全域'; var person = { myName: '小明', getName: function() { returnthis.myName; }, };
var getName = person.getName; console.log(getName(); /* Ans: 全域 */
第二題
1 2 3 4 5 6 7 8 9 10 11 12 13
var myName = '全域';
var obj = { myName: '奇怪的函式', fn: function(a, b, c) { returnthis.myName + ',' + a + ',' + b + ',' + c } } var fnA = obj.fn; var fnB = fnA.bind(null, 0);
console.log(fnB(1, 2)); /* Ans: 全域,0,1,2 */
第三題
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var value = 'Global';
var foo = { value: 'local', bar: function() { returnthis.value; } }