【发布时间】:2021-11-28 00:10:53
【问题描述】:
我想通过函数方法制作一个对象,因为我必须放入很多对象。我按照说明一步一步地做,但这种方法对我不起作用。我认为主要问题在于功能中的功能。我想让所有对象都有第二个函数的值,所以我命名为“A”、“B”、“C”、“D”字段。
function pol () {
this.name = name;
this.moc = moc;
this.square = square;
this.A = A;
this.B = B;
this.C = C;
this.D = D;
this.price = function(){
if(wynik1 >18000){ return A;}
if(wynik1 >10000){ return B;}
if(wynik1 >5000){ return C;}
if(wynik1>3000){ return D;}
})();
}
var Bingo = new pol ("Psyche", 370, 1.7, 4000, 4200, 4600, 5000);
var Ringo = new pol("Psyche Myche", 370, 1.7, 4000, 4200, 4600, 5000);
var Zingo = new pol("Psyche Ryche", 370, 1.7, 4000, 4200, 4600, 5000);
var moc = pol.Ringo.moc
var pow = pol.Ringo.square
var prices = pol.Ringo.price();
我在这一步有问题,但我的下一步是
var moc = function (){
if(k =="A"){return: pol.Ringo.moc}
if(k =="A"){return: pol.Bingo.moc}
if(k =="A"){return: pol.Zingo.moc}
}();
这可能吗?
【问题讨论】:
-
您的代码存在许多问题。回到说明并查看函数定义。您的
function pol() {...}没有分配任何参数。我很确定您希望它看起来像这样:function pol(name, moc, square, A, B, C, D) {...}。我认为this.price方法也存在同样的问题——似乎需要接受wynik1作为参数。至于最后一部分,您需要先完成第一部分,然后才能完成第二部分。 -
另外,您已经立即调用了函数并将结果分配给
.price,而不是分配函数来创建方法。
标签: javascript javascript-objects