2
var Ninja = function() {
   this.swingSword = function() {
       return true
   }
}
Ninja.swingFire = function() {
   return true
}

var ninja = new Ninja()
assert(ninja.swingFire()) // undefined

So it's creating a new object of Ninja, but why isn't swingFire included in this situation? Could someone explain why?

3
  • 2
    Because you have to use prototype with classes to add new methods/members Ninja.prototype.swingFire = function(). You can only use the direct assignment for object literals Commented Oct 8, 2013 at 19:23
  • 1
    @devnull69 The OP can use what they have, but they'll have to access it like Ninja.swingFire(), which I'm sure they don't want to do (a single, centralized method) Commented Oct 8, 2013 at 19:30
  • @devnull69 (I was just replying/referring to your "You can only use the direct assignment for object literals"...that was all...otherwise you're obviously right) Commented Oct 8, 2013 at 21:22

2 Answers 2

8

Instances created with new will inherit from constructor.prototype, not from the constructor object itself. This will behave as you want:

Ninja.prototype.swingFire = function() {
   return true;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Am I right to infer that this in this.swingSword context refers to the prototype then?
No, it will refer to the instance. But when you try to access a property and it's not found on the instance, then the prototype chain will be looked up.
Why else would I want to use this.swingSword to attach it to the instance as opposed to using Ninja.prototype.swingSword? I know shadowing the prototype function is one.
Usually, methods go into the prototype, so the same function object is shared by all instances. But sometimes you might want a method to have access to a private variable via closure, then you'll have to create it as an own property of the instance, from the constructor.
1

If you want to know how objects, functions, "classes" and inheritance in Javascript work, you should take a look at this video:

The Definitive Guide to Object-Oriented JavaScript: http://youtu.be/PMfcsYzj-9M

This is the best explanation of object orientation in JS that I have ever seen.

In your case, the following happens:

When you create a new Ninja(), you create a new empty object ({}) with a prototype property that points to the functions's prototype. Then the function Ninja will be called as the constructor for your newly created object. It creates the swingSword function and makes it a method of your object.

The swingFire function will not be assigned to your created object, because it is a method of the function itself, not its prototype. Functions are objects as well, so they can actually have properties.

If an object does not have a certain property or method, it looks at it's prototype. If the prototype has the said property/method, it uses it instead. That means that if you give the prototype a method, every object that uses it can use this method as well. That's why you assign methods to the prototype of a function.

Again: Watch the video and you will understand this explanation.

4 Comments

The prototype property of the constructor is not cloned into the instances; the instances just keep a reference to it.
@bfavaretto Ah, damn, you're right. This stuff just keeps confusing me >_< I corrected it.
You have confused me even more now..answer is too difficult to understand..I am saying that after using cons-prototypes few times!!!
@Onaseriousnote That's why I refer to the video explaining everything. Explaining prototypes and inheritance in Javascript is really difficult.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.