JavaScript中可调用对象的构造方法 [英] Constructor for callable object in JavaScript

查看:150
本文介绍了JavaScript中可调用对象的构造方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JavaScript中为可调用对象创建构造函数?

How can I make constructor for callable object in JavaScript?

我尝试了各种方法,如下所示。示例中只有实际对象的缩短示例。

I've attempted various ways, like following. The example there is just shortened example of actual object.

function CallablePoint(x, y) {
    function point() {
        // Complex calculations at this point
        return point
    }
    point.x = x
    point.y = y
    return point
}

这起作用,但它创建的对象不是 CallablePoint ,因此它不会从 CallablePoint.prototype 复制属性,并在上说出 false instanceof CallablePoint 。是否可以为可调用对象创建可工作的构造函数?

This works at first, but the object it creates isn't instance of CallablePoint, so it doesn't copy properties from CallablePoint.prototype and says false on instanceof CallablePoint. Is it possible to make working constructor for callable object?

推荐答案

当创建函数时,通过使用 function 语法或 Function 构造函数,它将获取内部 [[Call]] 属性。它不是函数本身的属性,而是任何函数在构造时获得的属性。

Turns out it's actually possible. When the function is created, either by using function syntax, or Function constructor, it gets internal [[Call]] property. It isn't a property of function itself, but rather property that any function gets when constructed.

这只意味着 [[调用]] 可能只有 Function 当它被构造时(好,有一个异常– Function.prototype 本身不从 Function 继承),这并不意味着它不能成为别的东西,同时保留 [[ Call]] 属性。嗯,只要你的浏览器不是 IE< 11

While that only means that anything with [[Call]] could be only Function when it's constructed (well, there is one exception – Function.prototype itself that doesn't inherit from Function), that doesn't mean it cannot become something else later, while preserving [[Call]] property. Well, provided your browser isn't IE < 11.

允许改变魔法的东西是来自ES6的 __ proto __ 已经在许多浏览器中实现。 __ proto __ 是一个包含当前原型的魔法属性。通过改变它,我可以使函数继承不是 Function 的东西。

The thing that allows changing the magic would be __proto__ from ES6, already implemented in many browsers. __proto__ is a magical property that contains current prototype. By changing it, I can make function that inherits from something that isn't Function.

function CallablePoint(x, y) {
    function point() {
        // Complex calculations at this point
        return point
    }
    point.__proto__ = CallablePoint.prototype
    point.x = x
    point.y = y
    return point
}
// CallablePoint should inherit from Function, just so you could use
// various function methods. This is not a requirement, but it's
// useful.
CallablePoint.prototype = Object.create(Function.prototype)

首先,构造函数 CallablePoint 使得函数(仅允许函数接下来,我改变它的原型,所以它将继承 CallablePoint 。在这个点我有一个函数不继承从函数(类型的混乱)。

First, the constructor for CallablePoint makes a Function (only Functions are allowed to begin with [[Call]] property. Next, I change its prototype, so it would inherit CallablePoint. At this point I have a function that doesn't inherit from Function (sort of confusing).

对于 CallablePoint ,我将 CallablePoint 的原型设置为 Function ,所以我有 CallablePoint 继承自 Function

After I defined constructor for CallablePoints, I set the prototype of CallablePoint to Function, so I have CallablePoint that inherits from Function.

这样, CallablePoint 实例具有继承链: CallablePoint - > Function - > Object 此外,由于对象是可调用的,因此根据规范, typeof 等于'function'

This way, the CallablePoint instances have inheritance chain: CallablePoint -> Function -> Object, while still being callable. Also, because the object is callable, it has according to the specification, typeof equal to 'function'.

这篇关于JavaScript中可调用对象的构造方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆