Twitter hashtag: #esspec
便利ツール
時事ネタ
もうすぐ締め切り
フロントエンドカンファレンス東京
JSConf
フロントエンドカンファレンス関西
tsgo preview
自己紹介 (近況報告)
syumai syumai.icon
Go / TSを書いて暮らしてます
肘の骨にヒビが入りました
iwatsurut
とくに、イベントもなく過ごしています。
仕事で、Go で Web システムを作ることになった。想定よりも複雑で、ちょとこまった。
igrep(山本悠滋)
前回のあらすじ
今回の範囲
Object.hasOwn
code:js
const o1 = {
a: 1,
};
console.log(Object.hasOwn(o1, "a"));
console.log(Object.hasOwn(o1, "b"));
console.log("a" in o1);
console.log("b" in o1);
const o2 = {
b: 2,
};
Object.setPrototypeOf(o2, o1);
console.log(Object.hasOwn(o2, "a"));
console.log(Object.hasOwn(o2, "b"));
console.log("a" in o2);
console.log("b" in o2);
/*
true
false
true
false
false // ここがinと違ってtrueにならない!
true
true
true
*/
Object.preventExtensions
code:js
const buffer1 = new ArrayBuffer(8);
const buffer2 = new ArrayBuffer(8, { maxByteLength: 16 });
console.log(buffer1.resizable);
// Expected output: true
console.log(buffer2.resizable);
// Expected output: false
console.log(Object.preventExtensions(new Uint8Array(buffer1)));
console.log(Object.preventExtensions(new Uint8Array(buffer2)));
/*
false
true
Uint8Array(8) 0, 0, 0, 0, 0, 0, 0, 0, buffer: ArrayBuffer(8), byteLength: 8, byteOffset: 0, length: 8, Symbol(Symbol.toStringTag): 'Uint8Array' Uncaught TypeError: Cannot prevent extensions
at Object.preventExtensions (<anonymous>)
at <anonymous>:11:23
*/
Object.prototype
%Object.prototype% はExtensible
code:js
"use strict";
console.log(Object.keys(Object.prototype));
Object.prototype.a = 1;
Object.prototype.b = 2;
console.log(Object.keys(Object.prototype));
console.log({}.a, {}.b);
class A {}
console.log(new A().a, new A().b);
console.log(Object.create(null).a, Object.create(null).b);
/*
[]
1 2
1 2
undefined undefined
*/
Object.prototypeへのObject.setPrototypeOfはnullしか通らない
code:js
console.log(Object.getPrototypeOf(Object.prototype));
const o = {
a: 1,
};
console.log(Object.setPrototypeOf(Object.prototype, o)); // NG
console.log(Object.setPrototypeOf(Object.prototype, null)); // OK
Object.prototype.constructor
実は値を差し替え可能
code:js
Object.prototype.constructor
Object.prototype.constructor = Function.prototype.constructor
Object.prototype.constructor
new Object()
{}
(new Object())()
Uncaught TypeError: (intermediate value) is not a function
Object.prototype.hasOwnProperty
code:js
({a:1}).hasOwnProperty("a");
true
({a:1}).hasOwnProperty("b");
false
({a:1}).hasOwnProperty("b");
false
Object.prototype.hasOwnProperty.bind(undefined)("a")
Uncaught TypeError: Cannot convert undefined or null to object
at hasOwnProperty (<anonymous>)
Object.prototype.hasOwnProperty.bind({})("a")
false
Object.prototype.hasOwnProperty.call({}, "a")
false
Object.prototype.hasOwnProperty.call({a:1}, "a")
true
Object.prototype.hasOwnProperty.call(null, "a")
Uncaught TypeError: Cannot convert undefined or null to object
at hasOwnProperty (<anonymous>)
Object.prototype.isPrototypeOf
code:js
const o1 = {};
undefined
const o2 = {};
undefined
const o3 = {};
undefined
Object.setPrototypeOf(o2, o1);
{}
Object.setPrototypeOf(o3, o2);
{}
o1.isPrototypeOf(o3);
true
Object.prototype.toLocaleString
code:js
({}).toLocaleString()
(10000).toLocaleString()
'10,000'
(10000).toString()
'10000'
Object.prototype
code:js
Object.prototype.toString.call(10000)
Object.prototype.toString.call(true)
Object.prototype.toString.call(undefined)
Object.prototype.toString.call(null)
Object.prototype.toString.call([])
Object.prototype.toString.call("")
Object.prototype.toString.call(new Date())
Object.prototype.toString.call(() => {})
Object.prototype.toString.call(1n)
Object.prototype.toString.call(new Uint8Array())
class A {}
undefined
Object.prototype.toString.call(new A())
Object.prototype.toString.call(async () => {})
'AsyncFunction'
undefined
o.toString()