JavaScript/constructor:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第3行: 第3行:
<blockquote><pre>
<blockquote><pre>
constructor([arguments]) { ... }</pre></blockquote>
constructor([arguments]) { ... }</pre></blockquote>
== 判断 ==
== 验证 ==
constructor 属性返回对创建此对象的数组函数的引用。
constructor 属性返回对创建此对象的数组函数的引用。


第11行: 第11行:
{'a':'1'}.constructor;
{'a':'1'}.constructor;
new Object.constructor;
new Object.constructor;
var a = new Object;
var foo = new Object;
a.constructor;
foo.constructor;
null.constructor;
undefined.constructor;
</syntaxhighlight>|{{consoleout|<pre>
</syntaxhighlight>|{{consoleout|<pre>
ƒ String() { [native code] }
ƒ String() { [native code] }
第19行: 第21行:
ƒ anonymous() {}
ƒ anonymous() {}
ƒ Object() { [native code] }
ƒ Object() { [native code] }
VM4070:1 Uncaught TypeError: Cannot read property 'constructor' of null
    at <anonymous>:1:6
VM4240:1 Uncaught TypeError: Cannot read property 'constructor' of undefined
    at <anonymous>:1:11
</pre>}}}}
</pre>}}}}
== 判断 ==
{{leftrightflex|<syntaxhighlight lang="javascript">
new String().constructor===String;
new Number().constructor===Number;
new Array().constructor===Array;
new Boolean().constructor===Boolean;
new Object().constructor===Object;
new Function().constructor===Function;
new RegExp().constructor===RegExp;
new Date().constructor===Date;
new Error().constructor===Error;
var foo = 1;
foo.constructor===Number;
BigInt('1').constructor === BigInt;
"str".constructor===String;
true.constructor===Boolean;
Symbol().constructor===Symbol;
</syntaxhighlight>|{{consoleout|<pre>
true
true
true
true
true
true
true
true
true
true
true
true
true
true
</pre>}}}}
== 继承 ==
== 兼容版本 ==
== 兼容版本 ==
{| class="wikitable sortable"
{| class="wikitable sortable"

2020年6月26日 (五) 06:17的版本

引言

constructor 是一种用于创建和初始化class创建的对象的特殊方法。[1]

constructor([arguments]) { ... }

验证

constructor 属性返回对创建此对象的数组函数的引用。

模板:Leftrightflex

判断

模板:Leftrightflex

继承

兼容版本

pc mobile server
chrome
Edge
Firefox
Internet Explorer
Opera
Safari
Android webview
Chrome for Android
Firefox for Android
Opera for Android
Safari on iOS
Samsung Internet
Node.js
49 13 45 NO 36 9 49 49 45 36 9 5.0 6.0.0