-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>模拟实现Symbol</title> | ||
</head> | ||
<body> | ||
<div> | ||
<h4> Symbol 函数前不能使用 new 命令,否则会报错。这是因为生成的 Symbol 是一个原始类型的值,不是对象。</h4> | ||
<h4>Symbol 值不能与其他类型的值进行运算,会报错。 </h4> | ||
</div> | ||
<script> | ||
var s = Symbol() | ||
console.log(s) // Symbol() | ||
console.log(typeof s) | ||
console.log(s instanceof Symbol) //false | ||
var s1 = Symbol('s1') | ||
console.log(s1) // Symbol(s1) | ||
let obj = { | ||
toString() { | ||
return 'toString' | ||
} | ||
} | ||
const sym = Symbol(obj) | ||
console.log(sym) // Symbol(toString | ||
var s2 = Symbol('s1') | ||
// Symbol 函数的参数只是表示对当前 Symbol 值的描述,相同参数的 Symbol 函数的返回值是不相等的。 | ||
console.log(s2 === s1) // false | ||
var ss = Symbol.for('sss') | ||
var ss2 = Symbol.for('sss') | ||
console.log(ss === ss2) //true | ||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function forOf(obj, cb) { | ||
let iterable, result | ||
if (typeof obj[Symbol.iterator] !== 'function') { | ||
throw new TypeError(obj + ' is not iterable') | ||
} | ||
if (typeof cb !== 'function') throw new TypeError('cb must bu calllable') | ||
iterable = obj[Symbol.iterator]() | ||
result = iterable.next() | ||
while (!result.done) { | ||
cb(result.value) | ||
result = iterable.next() | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
var generateName = (function () { | ||
var endfix = 0 | ||
return function (desc) { | ||
endfix++ | ||
return `@@${desc}_${endfix}` | ||
} | ||
})() | ||
|
||
var SymbolPolyfill = function Symbol(desc) { | ||
if (this instanceof SymbolPolyfill) throw new TypeError('Symbom is not a function') | ||
var descString = desc === undefined ? undefined : String(desc) | ||
var symbol = Object.create({ | ||
toString: function () { | ||
return this.__Name__ | ||
}, | ||
valueOf: function () { | ||
throw new Error('Cannot convert a Symbol value') | ||
} | ||
}) | ||
Object.defineProperties(symbol, { | ||
'__Description__': { | ||
value: descString, | ||
writable: false, | ||
enumerable: false, | ||
configurable: false | ||
}, | ||
'__Name__': { | ||
value: generateName(descString), | ||
writable: false, | ||
enumerable: false, | ||
configurable: false | ||
} | ||
}) | ||
return symbol | ||
} | ||
|
||
var forMap = {} | ||
Object.defineProperties(SymbolPolyfill, { | ||
'for': { | ||
value: function (desc) { | ||
var descString = desc === undefined ? undefined : String(desc) | ||
return forMap[descString] ? forMap[descString] : forMap[descString] = SymbolPolyfill(descString) | ||
}, | ||
writable: true, | ||
enumerable: false, | ||
configurable: true | ||
}, | ||
'keyFor': { | ||
value: function (symbol) { | ||
for (let key in forMap) { | ||
if(forMap[key] === symbol) return key | ||
} | ||
}, | ||
writable: true, | ||
enumerable: false, | ||
configurable: true | ||
} | ||
}) | ||
|
||
var a = SymbolPolyfill('a') | ||
var b = SymbolPolyfill('a') | ||
console.log(a === b) | ||
console.log(a.toString()) | ||
var obj = {} | ||
obj[a] = '1111' | ||
obj[b] = '222' | ||
console.log(obj) | ||
// console.log(a + 1) |
This file contains 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