【问题标题】:How to correctly put function in object Javascript by method?如何通过方法正确地将函数放入对象Javascript中?
【发布时间】: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


【解决方案1】:

请注意,当您运行时:

var Ringo = new pol("Psyche Myche", 370, 1.7, 4000, 4200, 4600, 5000); 

您已将输入添加到此 pol 构造函数中。按照这种逻辑,您还需要在 pol 构造函数中定义参数,例如:

function pol (name, moc, square, A, B, C, D) {...}

作为旁注,请注意以下行:var prices = pol.Ringo.price(); 您将this.price 定义为:

function(){
    if(wynik1 >18000){ return A;}
    if(wynik1 >10000){ return B;}
    if(wynik1 >5000){ return C;}
    if(wynik1>3000){ return D;}
})();

最后的括号使这个函数返回一个,而不是将this.price 设置为一个函数。你会注意到我复制了语法错误(函数关闭后的单括号),因为它决定了它是否会返回一个值。如果删除那个括号,则还需要删除 ()。决定 this.price 应该是一个函数还是一个值。基于您尝试过pol.Ringo.price() 的事实,您可能希望它改为类似函数 (还要注意wynik1 的添加参数(假设您随后调用Ringo.price(some number)):

this.price = function(wynik1) {
    if(wynik1 >18000){ return A;}
    if(wynik1 >10000){ return B;}
    if(wynik1 >5000){ return C;}
    if(wynik1>3000){ return D;}
}; // no additional parentheses

this.price 函数也不应该使用ABCD。它应该访问与 specific pol 构造函数连接的 specific 值:this.Athis.Bthis.Cthis.D

最后要注意的是pol.Ringo.moc的使用(或者你的任何其他参数都不会起作用。由于pol是一个构造函数,当你创建一个new pol时,没有连接到变量 polRingo 现在将指向复制所有信息的不同存储,然后根据您获得的参数设置new pol(...)。您可以访问任何新的@987654351 @ 构造只需使用变量名并执行以下操作:

var moc = Ringo.moc
var pow = Ringo.square
var prices = Ringo.price();

【讨论】:

    【解决方案2】:

    最后,在您的帮助下,我设法修复了它,但我不明白它为什么会起作用。

    var Ringo = new pol("Psyche", 370, 1.7, 4000, 4200, 4600, 5000); 
    var prices = Ringo.price(4000, 4200, 4600, 5000);
    

    Ringo.price(); 我可以输入任何数字,它就像 1、2、3、4 一样工作,但如果我输入“a”、“b”、“c”、“d”或空,则不要。为什么我不能只是放在那儿注意。

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 2015-07-12
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 2012-05-18
      • 2018-06-21
      相关资源
      最近更新 更多