您可以設置包含一個指向任何物體屬性變量:
var gEBTN = document.getElementsByTagName;
var gEBI = document.getElementById;
當你需要使用它,你只需要鍵入gETBN('div')...
,或gEBI('elementId')...
。你的情況:可製成
function changeIt() {
var ge = document.getElementsByTagName;
ge('div')[0].innerHTML = 'Changed';
}
一些掛鉤,以避免類型的長命令,對象鏈,並且可以藏漢改變其值(鍵入下面的摘錄在JS控制檯來查看結果):
改變在相同的範圍內
var originalObject = {
deepProperty: {
deepDeepProperty: {
someNumber: 0,
anotherNumber: 0,
someString: 'String',
anotherString: 'String',
someArray: ['a', 'b'],
anotherArray: ['a', 'b']
}
}
};
var hookA = originalObject.deepProperty;
var hookB = originalObject.deepProperty.deepDeepProperty;
hookA.siblingProperty = hookA;
hookB.someNumber = 1;
hookB.anotherNumber++;
hookB.someString = 'Strong';
hookB.anotherString.replace(/i/, 'o');
hookB.someArray = ['b', 'c'];
hookB.anotherArray.shift();
hookB.anotherArray.push('c');
console.log(originalObject);
console.log(hookA);
console.log(hookB);
console.log(hookA === originalObject.deepProperty);
console.log(hookB === originalObject.deepProperty.deepDeepProperty);
INSIDE改變一個FUNCTION
var originalObject = {
deepProperty: {
deepDeepProperty: {
someNumber: 0,
anotherNumber: 0,
someString: 'String',
anotherString: 'String',
someArray: ['a', 'b'],
anotherArray: ['a', 'b']
}
}
};
function changeOriginalObject(obj) {
var hookA = obj.deepProperty;
var hookB = obj.deepProperty.deepDeepProperty;
hookA.siblingProperty = hookA;
hookB.someNumber = 1;
hookB.anotherNumber++;
hookB.someString = 'Strong';
hookB.anotherString.replace(/i/, 'o');
hookB.someArray = [
'b',
'c'
];
hookB.anotherArray.shift();
hookB.anotherArray.push('c');
}
console.log(originalObject);
changeOriginalObject(originalObject);
console.log(originalObject);
這就是我一直在尋找的!很棒,連續第二次幫助我!謝謝。 –
['Function.prototype.bind'](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind)僅在ECMAScript 5(JavaScript 1.8.5)之後可用。 – Gumbo