newImage();// <img>Image();// Uncaught TypeError: Failed to construct 'Image': Please use the 'new' operator, this DOM object constructor cannot be called as a function.letmyFunc=()=>{};myFunc();// 可以正常执行newmyFunc();// TypeError: myFunc is not a constructor
2. 函数
对于使用 function 语法或 Function 构造器创建的对象来说,[[call]] 和 [[construct]] 的行为总是相似的,因为它们会执行同一段代码。
但是 [[construct]] 会比 [[call]] 多干几件事情:
首先,以 Object.prototype 为原型创建一个新对象 A
然后,以新对象 A 为 this 来执行函数的私有字段 [[call]]
如果 [[call]] 的返回值是对象 B,那么就返回该对象 B,否则就返回第 1 步中创建的对象 A
如果 [[construct]] 最终返回的是对象 B,那么对象 A 就成了在构造函数之外完全没法被访问的对象,这在一定程度上可以实现私有。示例代码如下: