简体   繁体   English

ECMAscript是否提供任何构造函数的方式?

[英]Does ECMAscript offer any ways of constructing functions?

As far as I know, constructors are unable to produce functions: they can assign properties to this , and offer an immediate prototype reference for further properties that are generic and thus not instance-specific. 据我所知,构造函数无法生成函数:它们可以为this分配属性,并为更通用的,因此不是特定于实例的其他属性提供即时prototype参考。 But it's impossible to assign anything to this directly. 但是,这是不可能的事情分配给this直接。 Even if it were, the logical outcome would be to replace the instance with the assignment, along with its prototype chain. 即使是这样,逻辑上的结果就是将实例及其原型链替换为赋值。

From what I have read of ES6 Classes, they amount to syntax sugar for grouping constructor declaration and prototype instantiation in a single statement. 根据我对ES6类的了解,它们相当于在单个语句中对构造函数声明和原型实例进行分组的语法糖。

My practical interest lies in the value of the instanceof operator in asserting that X conforms to the higher order description of Y without any duck-typing. 我的实际兴趣在于断言X符合Y的高阶描述而没有任何鸭类输入的instanceof运算符的价值。 In particular, duck-typing is undesirable because it relies on some kind of definition of Y which is external to Y itself. 特别地,鸭子类型是不希望的,因为它依赖于Y本身外部的某种Y定义。

Edit 编辑

I'm interested in functions which are instances of other functions 我对作为其他功能实例的功能感兴趣

In ECMAScript 6 you should be able to call Object.setPrototypeOf on a function , but it isn't advised and although in JavaScript a Function is an Object too you may end up with unexpected behaviour ECMAScript 6中,您应该能够在一个函数上调用Object.setPrototypeOf ,但是不建议这样做,尽管在JavaScript中Function也是一个Object ,但您可能最终会遇到意外的行为

function foo() {}
function bar() {}
Object.setPrototypeOf(bar, foo.prototype);
bar instanceof foo; // true
bar.constructor === foo; // true

I'm not entirely sure what you're asking, but hopefully these code examples will help you 我不确定您要问的是什么,但希望这些代码示例对您有帮助

Returning an Object from a function invoked with new 从用new调用的函数返回对象

function Foo() {
    // a constructor
}
function Bar() {
    // another constructor
    return new Foo();
}

var b = new Bar();
b instanceof Bar; // false
b instanceof Foo; // true

Using new Function 使用new Function

function Fizz() {
    return new Function('return "Buzz";');
}
var b = Fizz();
b(); // "Buzz"

Invoking a function with a different this by using call , apply or bind 调用一个函数用不同的this通过callapplybind

function hello() {
    return this;
}
hello(); // window, null or error depending on environment
hello.call({'foo': 'bar'});  // {'foo': 'bar'}
hello.apply({'foo': 'bar'}); // {'foo': 'bar'}
var b = hello.bind({'fizz': 'buzz'});
b(); // {'fizz': 'buzz'}

Extending a constructor 扩展构造函数

function Foo() {
    this.foo = 'foo';
}
Foo.prototype = {'fizz': 'buzz'};

function Bar() {
    Foo.call(this);
    this.bar = 'bar';
}
// and link in inheritance
Bar.prototype = Object.create(Foo.prototype);

var b = new Bar();
b.bar; // "bar"
b.foo; // "foo"
b.fizz; // "buzz"

b instanceof Bar; // true
b instanceof Foo; // true
// but
Bar instanceof Foo; // false

Constructors can construct functions. 构造函数可以构造函数。 If your constructor returns an object, the object returned by the constructor function becomes the result of the whole new expression. 如果构造函数返回一个对象,则构造函数返回的对象将成为整个new表达式的结果。

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new 参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

As functions are objects, you can return them just as well from your constructor as shown here: 由于函数是对象,因此您也可以从构造函数中返回它们,如下所示:

function Shout(text) {
  return function () {
    alert(text);
  };
 }

shout1 = new Shout('hola');
shout2 = new Shout('eeee');

shout1();  // hola
shout2();  // eeee

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM