Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Oh boy, you are confusing quite a few things. </p> <pre><code>function CarConstructor(){ this.speed=19; // in mph this.make="Ford"; this.fillKph; // &lt;-&gt; This particular statement has no meaning. //When you write this.fillKph without any assignment, it will be 'undefined'. //Just because you have a function named 'fillKph' somewhere else, //it doesn't mean it will get attached to this property. } </code></pre> <p>Try,</p> <pre><code>var toyota = new Car(); alert(typeof toyota.fillKph); //will alert undefined. </code></pre> <p>The fillKph function is created in global scope, i.e. as property of 'Window' object.</p> <pre><code>function fillKph(){ var me=this; $("#kphdiv").html(me.speed*1.61); } </code></pre> <p>To fix it, you can what rezzif suggested. Your final code will look like</p> <pre><code>function Car() { this.speed=19; // in mph this.make="Ford"; this.fillKph = function (){ $("#kphdiv").html(this.speed*1.61); }; } car1 = new Car(); car1.fillKph(); </code></pre> <p>If you notice, I did not store reference to 'this' inside a local variable. Why? There is no need in this scenario. To understand more, see <a href="https://stackoverflow.com/questions/1007340/javascript-function-aliasing-doesnt-seem-to-work/1162192#1162192">my detailed answer here</a>. </p> <p>If you are going to create lot of Car objects, you can define the fillKph method on the prototype.</p> <pre><code>function Car() { this.speed=19; // in mph this.make="Ford"; } Car.prototype.fillKph = function fillKph() { $("#kphdiv").html(this.speed*1.61); }; car1 = new Car(); car1.fillKph(); </code></pre> <p><strong>EDIT:</strong></p> <p>If you do something like,</p> <pre><code>function CarConstructor(){ this.speed=19; // in mph this.make="Ford"; this.fillKph = fillKph; } function fillKph(){ $("#kphdiv").html(me.speed*1.61); } car1 = new Car(); car1.fillKph(); //This will work as expected. </code></pre> <p>But the problem is that fillKph is defined in 'Window' scope, so I can directly call it like,</p> <pre><code>fillKph(); //Calling it this way will break it as it won't get correct 'this'. </code></pre> <p>Point is,</p> <pre><code>alert(typeof fillKph); // alerts 'function' if you do it your way, alert(typeof fillKph); // alerts 'undefined', if you do it the way I suggested, which is preferred in my opinion. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload