1

The classic way to create constructor function is like:

var Obj = function(param){
    this.param = param;
    this.method = function(){ console.log(this.param); }
}

But why i can't do something like this:

Obj.anotherMethod = function(){ //some code }

(I know about Obj.prototype.anotherMethod).

In practical usage i can't understand why use String.prototype.func instead of String.func to define new method. Is it just because String is constructor function and it's impossible to add method to it?

2
  • You're only making a single object of this type. Try making two objects of that type and then see if your method is as streamlined (hint, you will want to use the new operator and you will need methods on the new object, not on the function itself). Commented Aug 17, 2014 at 22:01
  • stackoverflow.com/questions/16063394/… Commented Aug 18, 2014 at 2:28

3 Answers 3

1

Those are different things.

If you add a method to Constructor.prototype, it will be available on all instances of Constructor.

You can also add methods to Constructor itself. However, instances won't inherit them.

For example,

  • charAt method is defined on String.prototype, so you can use both "abc".charAt(1) and String.prototype.charAt.call("abc", 1).

  • But fromCharCode is defined on String, so you can use String.fromCharCode(65), but "abc".fromCharCode is undefined.

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

3 Comments

Is fromCharCode usage is like class static method? And why it is not in prototype?
@Maxx It isn't in the prototype because it makes no sense to run it on string instances. Not sure if it is like class static methods because I only know prototypical inheritance, not class inheritance.
fromCharCode is static method that does not require object instance. Like almost every methods from Math
0

You can do this with prototypal inheritance. Instead of using constructors, you can have objects inherit from other objects using Object.create(). You can add members to the parent object at any time and they will be available to objects that have the parent as their prototype.

var Base = {a: "a"};
Base.b = "b";
var sub = Object.create(Base); // creates a new object with Base as its prototype
Base.c = "c"; // sub.c will be available because Base is its prototype!
alert(sub.a + sub.b + sub.c); // "abc"

http://jsfiddle.net/kqtm4f8j/

Great article to understand prototypal inheritance patterns:

http://aaditmshah.github.io/why-prototypal-inheritance-matters/

Comments

0

Here's a little code that should help. Starting with your constructor:

var Obj = function(param){
    this.param = param;
    this.method = function(){ console.log(this.param); }
}

Now make an instance:

var inst = new Obj("testParam")
//now use it
console.log(inst.param) // logs: "testparam

Now add another parameter to Obj:

Obj.anotherParam = "TestParam2"
console.log(Obj.anotherParam) // <-- Works
console.log(inst.anotherParam) // <-- but this is Undefined on the instance

But if you add it to the prototype:

Obj.prototype.anotherParam = "TestParam2"
console.log(inst.anotherParam) // <-- this now works as expected

The takeaway is that when you add it to the prototype it becomes available to all instances. If you simply add it to the constructor, only the constructor function has the parameter. There are a lot of other ways to do this in Javascript, but understanding the prototype object is a good place to start.

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.