简体   繁体   English

在MDN网站上尝试示例时出现语法错误

[英]Syntax error on trying an example from MDN website

I was trying out a sample code for apply() from Mozilla Developer Network ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript ) 我正在尝试从Mozilla开发人员网络( https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript )中的apply()示例代码

The code I write is as below:- 我写的代码如下:

function Person( first, last ) {
    this.first = first;
    this.last = last;
}

Person.prototype.fullName = function() {
    return this.first+"; "+this.last;
};

var p1 = new Person( "Junaid", "Kirkire" );
var p2 = new Person( "Aditya", "Shanker" );

Person.prototype.toString = function() {
    return "Name " + this.first;
};

function trivialNew( constructor, ...args ) {
    var o = {};
    constructor.apply( o, ...args );
    return o;
};

var p3 = trivialNew( Person, "Junaid", "Kirkire" );

I am getting a SyntaxError on the line constructor.apply(). 我在行构造器.apply()上遇到了SyntaxError。 Can anyone help me out with this? 谁能帮我这个忙吗? Thanks. 谢谢。

...args is not valid JavaScript syntax. ...args是无效的JavaScript语法。 It's MDN's way of saying "This is where your arguments go" 这是MDN所说的“这就是您的论点所在”

(So, replace that with your actual arguments) (因此,将其替换为您的实际参数)

For more information, check the MDN documentation on Function.prototype.apply() 有关更多信息,请查看Function.prototype.apply()上的MDN文档。

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

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