gpt4 book ai didi

javascript - Javascript 中函数作为原型(prototype)对象的属性?

转载 作者:行者123 更新时间:2023-12-03 10:05:28 26 4
gpt4 key购买 nike

我有一个类似的代码:

var thing = function (prop1, prop2, prop3) {
this.prop1 = prop1;
this.prop2 = prop2;
this.prop3 = prop3;
};

function assignProp1 () {
return 'value1'; //Function simplified for the question
}

function generateThing () {
return new thing(function () {return assignProp1();}, 'value2', 'value3');
}

我想做的是从原型(prototype)“事物”创建不同的对象。但代码并没有接受“value1”作为 prop1,而是返回“generateThing/<()”作为 prop1。我不明白为什么它不起作用。

最佳答案

var thing = function (prop1, prop2, prop3) {
this.prop1 = prop1;
this.prop2 = prop2;
this.prop3 = prop3;
};

function assignProp1 () {
return 'value1'; //Function simplified for the question
}

function generateThing () {
return new thing( assignProp1(), 'value2', 'value3');
}

关于javascript - Javascript 中函数作为原型(prototype)对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30377803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com