- class BoringClass
- end
- class CoolClass
- def ==(other_object)
- other_object.is_a? CoolClass
- end
- end
- BoringClass.new == BoringClass.new #=> false
- CoolClass.new == CoolClass.new #=> true!
- Symbol(); // symbol
- console.log(Symbol()); // 输出 "Symbol()" 至控制台
- assert(typeof Symbol() === 'symbol') new Symbol(); // TypeError: Symbol is not a constructor
- console.log(Symbol('foo')); // 输出 "Symbol(foo)" 至控制台
- assert(Symbol('foo').toString() === 'Symbol(foo)');
- var myObj = {};
- var fooSym = Symbol('foo');
- var otherSym = Symbol('bar');
- myObj['foo'] = 'bar';
- myObj[fooSym] = 'baz';
- myObj[otherSym] = 'bing';
- assert(myObj.foo === 'bar');
- assert(myObj[fooSym] === 'baz');
- assert(myObj[otherSym] === 'bing');
- var fooSym = Symbol('foo');
- var myObj = {};
- myObj['foo'] = 'bar';
- myObj[fooSym] = 'baz';
- Object.keys(myObj); // -> [ 'foo' ]
- Object.getOwnPropertyNames(myObj); // -> [ 'foo' ]
- Object.getOwnPropertySymbols(myObj); // -> [ Symbol(foo) ]
- assert(Object.getOwnPropertySymbols(myObj)[0] === fooSym);
- assert.notEqual(Symbol(), Symbol());
- assert.notEqual(Symbol('foo'), Symbol('foo'));
- assert.notEqual(Symbol('foo'), Symbol('bar'));
- var foo1 = Symbol('foo');
- var foo2 = Symbol('foo');
- var object = { [foo1] : 1,
- [foo2] : 2,
- };
- assert(object[foo1] === 1);
- assert(object[foo2] === 2);
- assert.notEqual(Symbol('foo'), Symbol('foo'));
- assert.equal(Symbol.for('foo'), Symbol.for('foo'));
- // 不是唯一的:
- var myObj = {};
- var fooSym = Symbol.for('foo');
- var otherSym = Symbol.for('foo');
- myObj[fooSym] = 'baz';
- myObj[otherSym] = 'bing';
- assert(fooSym === otherSym);
- assert(myObj[fooSym] === 'bing');
- assert(myObj[otherSym] === 'bing');
- // 跨域
- iframe = document.createElement('iframe');
- iframe.src = String(window.location);
- document.body.appendChild(iframe);
- assert.notEqual(iframe.contentWindow.Symbol, Symbol);
- assert(iframe.contentWindow.Symbol.for('foo') === Symbol.for('foo')); // true!
- var localFooSymbol = Symbol('foo');
- var globalFooSymbol = Symbol.for('foo');
- assert(Symbol.keyFor(localFooSymbol) === undefined);
- assert(Symbol.keyFor(globalFooSymbol) === 'foo');
- assert(Symbol.for(Symbol.keyFor(globalFooSymbol)) === Symbol.for('foo'));
- log.levels = {
- DEBUG: Symbol('debug'),
- INFO: Symbol('info'),
- WARN: Symbol('warn'),
- };
- log(log.levels.DEBUG, 'debug message');
- log(log.levels.INFO, 'info message');
- var size = Symbol('size');
- class Collection {
- constructor() {
- this[size] = 0;
- }
- add(item) {
- this[this[size]] = item;
- this[size]++;
- }
- static sizeOf(instance) {
- return instance[size];
- }
- }
- var x = new Collection();
- assert(Collection.sizeOf(x) === 0);
- x.add('foo');
- assert(Collection.sizeOf(x) === 1);
- assert.deepEqual(Object.keys(x), ['0']);
- assert.deepEqual(Object.getOwnPropertyNames(x), ['0']);
- assert.deepEqual(Object.getOwnPropertySymbols(x), [size]);
- // 从 API 的 Symbols 常量中获得这个充满魔力的 Inspect Symbol
- var inspect = console.Symbols.INSPECT;
- var myVeryOwnObject = {};
- console.log(myVeryOwnObject); // 日志 `{}`
- myVeryOwnObject[inspect] = function () { return 'DUUUDE'; };
- console.log(myVeryOwnObject); // 日志输出 `DUUUDE`
- console.log = function(…items) {
- var output = '';
- for (const item of items) {
- if (typeof item[console.Symbols.INSPECT] === 'function') {
- output += item[console.Symbols.INSPECT](item);
- } else {
- output += console.inspect[typeof item](item);
- }
- output += ' ';
- }
- process.stdout.write(output + '\n');
- }
- class MyClass {
- static[Symbol.hasInstance](lho) {
- return Array.isArray(lho);
- }
- }
- assert([] instanceof MyClass);
- var myArray = [1, 2, 3];
- // 使用 `for of` 的实现
- for (var value of myArray) {
- console.log(value);
- }
- // 没有 `for of` 的实现
- var _myArray = myArray[Symbol.iterator]();
- while (var _iteration = _myArray.next()) {
- if (_iteration.done) {
- break;
- }
- var value = _iteration.value;
- console.log(value);
- }
- class Collection {
- *[Symbol.iterator]() {
- var i = 0;
- while(this[i] !== undefined) {
- yield this[i];
- ++i;
- }
- }
- }
- var myCollection = new Collection();
- myCollection[0] = 1;
- myCollection[1] = 2;
- for(var value of myCollection) {
- console.log(value); // 1, then 2
- }
- x = [1, 2].concat([3, 4], [5, 6], 7, 8);
- assert.deepEqual(x, [1, 2, 3, 4, 5, 6, 7, 8]);
- class
- ArrayIsh
- extends
- Array
- {
- get [Symbol.isConcatSpreadable]() {
- return true;
- }
- }
- class
- Collection
- extends
- Array
- {
- get [Symbol.isConcatSpreadable]() {
- return false;
- }
- }
- arrayIshInstance = new ArrayIsh();
- arrayIshInstance[0] = 3;
- arrayIshInstance[1] = 4;
- collectionInstance = new Collection();
- collectionInstance[0] = 5;
- collectionInstance[1] = 6;
- spreadableTest = [1,2].concat(arrayInstance).concat(collectionInstance);
- assert.deepEqual(spreadableTest, [1, 2, 3, 4, <Collection>]);
- var keys = [];
- with(Array.prototype) {
- keys.push('foo');
- }
- Object.keys(Array.prototype[Symbol.unscopables]); // -> ['copyWithin', 'entries', 'fill', 'find', 'findIndex', 'keys']
- // 不使用 unscopables:
- class MyClass {
- foo() { return 1; }
- }
- var foo = function () { return 2; };
- with (MyClass.prototype) {
- foo(); // 1!!
- }
- // 使用 unscopables:
- class MyClass {
- foo() { return 1; }
- get [Symbol.unscopables]() {
- return { foo: true };
- }
- }
- var foo = function () { return 2; };
- with (MyClass.prototype) {
- foo(); // 2!!
- }
- class MyMatcher {
- constructor(value) {
- this.value = value;
- } [Symbol.match](string) {
- var index = string.indexOf(this.value);
- if (index === -1) {
- return null;
- }
- return [this.value];
- }
- }
- var fooMatcher = 'foobar'.match(new MyMatcher('foo'));
- var barMatcher = 'foobar'.match(new MyMatcher('bar'));
- assert.deepEqual(fooMatcher, ['foo']);
- assert.deepEqual(barMatcher, ['bar']);
- class MyReplacer {
- constructor(value) {
- this.value = value;
- }
- [Symbol.replace](string, replacer) {
- var index = string.indexOf(this.value);
- if (index === -1) {
- return string;
- }
- if (typeof replacer === 'function') {
- replacer = replacer.call(undefined, this.value, string);
- }
- return
- `
- ${string.slice(0, index)}
- ${replacer}
- ${string.slice(index + this.value.length)}
- `
- ;
- }
- }
- var fooReplaced = 'foobar'.replace(new MyReplacer('foo'), 'baz');
- var barMatcher = 'foobar'.replace(new MyReplacer('bar'), function () { return 'baz' });
- assert.equal(fooReplaced, 'bazbar');
- assert.equal(barReplaced, 'foobaz');
- class MySearch {
- constructor(value) {
- this.value = value;
- } [Symbol.search](string) {
- return string.indexOf(this.value);
- }
- }
- var fooSearch = 'foobar'.search(new MySearch('foo'));
- var barSearch = 'foobar'.search(new MySearch('bar'));
- var bazSearch = 'foobar'.search(new MySearch('baz'));
- assert.equal(fooSearch, 0);
- assert.equal(barSearch, 3);
- assert.equal(bazSearch, -1);
- class MySplitter {
- constructor(value) {
- this.value = value;
- } [Symbol.split](string) {
- var index = string.indexOf(this.value);
- if (index === -1) {
- return string;
- }
- return [string.substr(0, index), string.substr(index + this.value.length)];
- }
- }
- var fooSplitter = 'foobar'.split(new MySplitter('foo'));
- var barSplitter = 'foobar'.split(new MySplitter('bar'));
- assert.deepEqual(fooSplitter, ['', 'bar']);
- assert.deepEqual(barSplitter, ['foo', '']);
- Array.prototype.map = function(callback) {
- var returnValue = new Array(this.length);
- this.forEach(function(item, index, array) {
- returnValue[index] = callback(item, index, array);
- });
- return returnValue;
- }
- Array.prototype.map = function(callback) {
- var Species = this.constructor[Symbol.species];
- var returnValue = new Species(this.length);
- this.forEach(function(item, index, array) {
- returnValue[index] = callback(item, index, array);
- });
- return returnValue;
- }
- class
- Foo
- extends
- Array
- {
- static get [Symbol.species]() {
- return this;
- }
- }
- class
- Bar
- extends
- Array
- {
- static get [Symbol.species]() {
- return Array;
- }
- }
- assert(new Foo().map(function(){}) instanceof Foo);
- assert(new Bar().map(function(){}) instanceof Bar);
- assert(new Bar().map(function(){}) instanceof Array);
- class
- TimeoutPromise
- extends
- Promise
- {
- static get [Symbol.species]() {
- return Promise;
- }
- }
- class AnswerToLifeAndUniverseAndEverything {
- [Symbol.toPrimitive](hint) {
- if (hint === 'string') {
- return 'Like, 42, man';
- } else if (hint === 'number') {
- return 42;
- } else {
- // 大多数类(除了 Date)都默认返回一个数值原始值
- return 42;
- }
- }
- }
- var answer = new AnswerToLifeAndUniverseAndEverything();
- +answer === 42;
- Number(answer) === 42;
- ''+answer === 'Like, 42, man';
- String(answer) === 'Like, 42, man';
- class Collection {
- get[Symbol.toStringTag]() {
- return 'Collection';
- }
- }
- var x = new Collection();
- Object.prototype.toString.call(x) === '[object Collection]'
来源: https://juejin.im/post/5a0e65c1f265da430702d6b9