JavaScript中的Object.getOwnPropertySymbols()方法是標準的內置對象,該對象返回給定對象中存在的所有符號屬性的數組。
用法:
Object.getOwnPropertySymbols(obj)
參數:
- obj:此參數是要返回其符號屬性的對象。
返回值:此方法返回所有符號屬性的數組,這些符號屬性與直接在給定對象中找到的屬性相對應。
這是此方法的示例
範例1:
<script>
const object1 = {};
let vala = Symbol('geek1');
let valb = Symbol.for('geek2');
object1[vala] = 'localSymbol';
object1[valb] = 'globalSymbol';
const objectSymbols = Object.getOwnPropertySymbols(object1);
console.log(objectSymbols.length);
const object2 = {};
let a = Symbol('a');
let b = Symbol.for('b');
const objectSymbols1 = Object.getOwnPropertySymbols(object2);
console.log(objectSymbols1.length);
</script>
輸出:
2 0
範例2:
<script>
const object1 = {};
let vala = Symbol('geek1');
let valb = Symbol.for('geek2');
let valc = Symbol.for('geek3');
object1[vala] = 'localSymbol';
object1[valb] = 'globalSymbol';
object1[valc] = 'globalSymbol';
const objectSymbols = Object.getOwnPropertySymbols(object1);
console.log(objectSymbols.length);
console.log(objectSymbols);
console.log(objectSymbols[0]);
console.log(objectSymbols[2]);
console.log(objectSymbols[1]);
</script>
輸出:
3 Array [Symbol(geek1), Symbol(geek2), Symbol(geek3)] Symbol(geek1) Symbol(geek3) Symbol(geek2)
支持的瀏覽器:
- 穀歌瀏覽器
- Firefox
- IE
- Opera
- Safari
- Edge
相關用法
- Javascript padStart()用法及代碼示例
- Javascript Int8Array from()用法及代碼示例
- Javascript Reflect.get()用法及代碼示例
- Javascript Reflect.has()用法及代碼示例
- Javascript Unit16Array.from()用法及代碼示例
- Javascript handler.get()用法及代碼示例
- Javascript Int16Array from()用法及代碼示例
- Javascript Uint8Array.from()用法及代碼示例
- Javascript hasOwnProperty()用法及代碼示例
- Javascript compile()用法及代碼示例
- Javascript Uint32Array from()用法及代碼示例
- Javascript Array from()用法及代碼示例
- Javascript Array.from()用法及代碼示例
- Javascript handler.has()用法及代碼示例
- Javascript exec()用法及代碼示例
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 JavaScript | Object.getOwnPropertySymbols() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。