简体   繁体   English

如何调用对象的方法?

[英]How do I call a method to an object?

How would I call my method to my other objects?我如何将我的方法调用到我的其他对象? Been having lots of trouble with everything I've tried.我尝试过的一切都遇到了很多麻烦。 I'm not that confident with this stuff, just looking on how to tell if the object is safe to drive or not.我对这些东西没有那么自信,只是想看看如何判断该物体是否可以安全驾驶。

//Create a constructor function called `Track`. It will accept two parameters - the name of the track and the maximum capacity of the track. 
let track = function(name, capacity){
    this.trackName=name
    this.personnel=0;
    this.cars=[];
    this.cap=capacity;
}


//We'll need a value for the average weight of a person but this value will be the same for all tracks. 
//Add a property `personWeight` on the `Track` prototype so that all instances share the same value. 
track.prototype.personWeight = 200

//Create three methods on the prototype that will calculate track weight, person weight, and if its safe to drive
    function personWeight(){
        personnelWeight = this.personWeight * this.personnel
        return personnelWeight
    }

    function trackWeight(){
        let carsTotal = function myFunc(total, num) {
        return total - num;
    }
        let weightTotal = (this.personnel * this.personWeight) + (this.carsTotal)
        return weightTotal
    }

    function safeToDrive(){
        if(this.trackWeight<this.capacity){
            return true 
        }
    }


//Create two track objects
let trackOne = new track ("Daytona", 25000);
trackOne.cars = [1800, 2400, 2700, 3200, 3600, 3800, 4200]
trackOne.personnel = 10

let trackTwo = new track ("Indiana",15000);
trackTwo.cars = [2000, 2300, 2800, 3000, 3500, 3700, 4000]
trackTwo.personnel = 8


//Call the `safeToDrive` method for truck objects. 

With the code as it is now, you would use safeToDrive.call(trackOne) .使用现在的代码,您将使用safeToDrive.call(trackOne) However, this is not the straight-forward way you would do it normally.但是,这不是您通常会执行的直接方式。

I guess what you really want is assigning these methods to the prototype:我猜你真正想要的是将这些方法分配给原型:

    track.prototype.safeToDrive = function () {
        if(this.trackWeight<this.capacity){
            return true 
        }
    }

Then you'd call them using trackOne.safeToDrive() .然后你可以使用trackOne.safeToDrive()调用它们。

The same goes for personWeight and trackWeight . personWeighttrackWeight


A few other observations:其他一些观察:

  1. Your check for this.capacity won't work because the property is actually called cap and not capacity according to what you set in your constructor.您对this.capacity检查将不起作用,因为根据您在构造函数中设置的内容,该属性实际上被称为cap而不是capacity

  2. safeToDrive currently returns true or nothing, ie undefined , and not true or false as you would expect. safeToDrive当前返回true或不返回,即undefined ,而不是您期望的truefalse

    You could fix that by either adding an else with return false or simply using return this.trackWeight < this.capacity instead of the whole if condition.您可以通过添加带有return falseelse或简单地使用return this.trackWeight < this.capacity而不是整个if条件来解决这个问题。

  3. Oh, also, your personnelWeight variable is accidentally made global.哦,还有,您的personnelWeight变量被意外设为全局变量。 Add a let before it.在它之前添加一个let To avoid this in the first place, add 'use strict' at the top of your file to get warned about this issue next time.为了首先避免这种情况,请在文件顶部添加'use strict' ,以便下次收到有关此问题的警告。

  4. I'm not sure what you are doing with carsTotal there though, I guess that should be a member function as well (otherwise you couldn't even call it using this.carsTotal as you do now).我不确定你在那里用carsTotal做什么,我想这也应该是一个成员函数(否则你甚至不能像现在一样使用this.carsTotal调用它)。 Plus, your indention is wrong there.另外,你的缩进是错误的。 (Put your file through a beautifier to see what I mean.) (把你的文件通过美化器看看我的意思。)

  5. Do you mean truck instead of track maybe...?你的意思是truck而不是track也许......?

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

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