JavaScript/constructor

来自康健生活
Kangkk​(留言 | 贡献)2026年11月26日 (四) 05:43的版本 (文字替换 -“{{Leftrightflex|”替换为“{{Double-columns|”)

引言

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

判断

模板: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