JavaScript/constructor:修订间差异
来自康健生活
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” 标签:ReplaceText 移动版编辑 移动版网页编辑 |
|||
| (未显示同一用户的6个中间版本) | |||
| 第3行: | 第3行: | ||
<blockquote><pre> | <blockquote><pre> | ||
constructor([arguments]) { ... }</pre></blockquote> | constructor([arguments]) { ... }</pre></blockquote> | ||
== | |||
ES2015 为没有指定构造函数的类提供了默认构造函数。 | |||
== 验证 == | |||
constructor 属性返回对创建此对象的数组函数的引用。 | constructor 属性返回对创建此对象的数组函数的引用。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
'ff'.constructor; | 'ff'.constructor; | ||
[1,2].constructor; | [1,2].constructor; | ||
{'a':'1'}.constructor; | {'a':'1'}.constructor; | ||
new Object.constructor; | new Object.constructor; | ||
var | var foo = new Object; | ||
foo.constructor; | |||
null.constructor; | |||
undefined.constructor; | |||
</syntaxhighlight>|{{consoleout|<pre> | </syntaxhighlight>|{{consoleout|<pre> | ||
ƒ String() { [native code] } | ƒ String() { [native code] } | ||
| 第19行: | 第24行: | ||
ƒ 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>}}}} | |||
== 判断 == | |||
{{Double-columns|<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; | |||
new Map().constructor===Map; | |||
new Set().constructor===Set; | |||
var puz = new Promise((resolve,reject)=>{}); | |||
puz constructor===Promise; | |||
var xyz = new Proxy({},{}); | |||
xyz.constructor===Proxy | |||
</syntaxhighlight>|{{consoleout|<pre> | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
true | |||
false | |||
</pre>}}}} | </pre>}}}} | ||
== 继承 == | |||
== 兼容版本 == | |||
{| class="wikitable sortable" | |||
|- | |||
! colspan=6| pc !! colspan=6| mobile !! server | |||
|- | |||
! {{Vertical header|stp=1|chrome}} | |||
! {{Vertical header|stp=1|Edge}} | |||
! {{Vertical header|stp=1|Firefox}} | |||
! {{Vertical header|stp=1|Internet Explorer}} | |||
! {{Vertical header|stp=1|Opera}} | |||
! {{Vertical header|stp=1|Safari}} | |||
! {{Vertical header|stp=1|Android webview}} | |||
! {{Vertical header|stp=1|Chrome for Android}} | |||
! {{Vertical header|stp=1|Firefox for Android}} | |||
! {{Vertical header|stp=1|Opera for Android}} | |||
! {{Vertical header|stp=1|Safari on iOS}} | |||
! {{Vertical header|stp=1|Samsung Internet}} | |||
! {{Vertical header|stp=1|Node.js}} | |||
|- | |||
| 49 || 13 || 45 || NO || 36 || 9 || 49 || 49 || 45 || 36 || 9 || 5.0 || 6.0.0 | |||
|} | |||
2026年11月26日 (四) 08:35的最新版本
引言
constructor 是一种用于创建和初始化class创建的对象的特殊方法。[1]
constructor([arguments]) { ... }
ES2015 为没有指定构造函数的类提供了默认构造函数。
验证
constructor 属性返回对创建此对象的数组函数的引用。
'ff'.constructor;
[1,2].constructor;
{'a':'1'}.constructor;
new Object.constructor;
var foo = new Object;
foo.constructor;
null.constructor;
undefined.constructor;
ƒ String() { [native code] }
ƒ Array() { [native code] }
VM317:1 Uncaught SyntaxError: Unexpected token ':'
ƒ anonymous() {}
ƒ 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
判断
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;
new Map().constructor===Map;
new Set().constructor===Set;
var puz = new Promise((resolve,reject)=>{});
puz constructor===Promise;
var xyz = new Proxy({},{});
xyz.constructor===Proxy
true true true true true true true true true true true true true true true true true false
继承
兼容版本
| pc | mobile | server | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 49 | 13 | 45 | NO | 36 | 9 | 49 | 49 | 45 | 36 | 9 | 5.0 | 6.0.0 |
- ↑ constructor-JavaScript|MDN[EB/OL].https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes/constructor
No categories