1

I'm learning Javascript from Head First Javascript (Morrison).

In one example, the author declares a variable 'signature' to be a class property of a class called Blog by doing the following:

    Blog.prototype.signature = "by Blogger-Name";

Is there difference between the above and declaring it like how I have below?

    Blog.signature = "by Blogger-Name";
3
  • @vinayakj It isn't because my question is not about whether a function is referenced or copied. Commented May 1, 2015 at 6:14
  • @vinayakj I did go through that post and did not see my answer. if you still think my question is answered there, please be more specific. Commented May 1, 2015 at 6:40
  • @vinayakj While you have answered my question, the post you referred me to does not. I'll just leave it at that. Commented May 1, 2015 at 6:58

2 Answers 2

3

All instances of Blog will have signature when you using .prototype. So when you instantiate var blog = new Blog. It will have a signature property.

If you were to just use Blog.signature = x Then when you create an object with new it wouldn't be there.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, but all instances can access the signature property through Blog.signature anyway. So what is the point of declaring properties through .prototype then?
if you use both ways, then yes you can access it either way, but if you choose to use .prototype then you can think of it as a class member
True, but why would you declare any variable that remains the same value for all instances of a class as an instance variable? You would only declare it as a class/static variable... I should probably change my question at this point to: Is there a scenario where you would be required to declare a variable using .prototype?
I'd recommend asking a new question if you were planning on changing it.
Yes, I think I'll do that.
1

Setting a property of a prototype member of a constructor makes the property visible (when reading) from all instances.

This can be done to emulate static members of C++ classes and is often done in Javascript form methods (because methods are indeed just regular data members, differently from C++).

One example where this could be used instead for "data" is keeping all instances of a certain "class" in a container:

function Widget(x0, y0, x1, y1, name) {
    this.x0 = x0;
    this.y0 = y0;
    this.x1 = x1;
    this.y1 = y1;
    this.name = name;
    this.all_instances.push(this);
}

Widget.prototype.all_instances = [];

var w1 = new Widget(10, 20, 30, 40, "First");
var w2 = new Widget(10, 20, 30, 40, "Second");

Note that however when writing to such a "static" property you're creating instead an instance property; push works because it's not writing to .all_instances but modifying its content (the array).

Comments

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.