2
function abc(){
 this.a = "Hey this is A";   
}

var va = new abc();

va.prototype = function(){
  this.b = 'Hey b is added';
}

console.log(va.b);

va.b is undefined, I thought I already added it to va? since console.log(va) is an object if I console before the prototype line. What's wrong with my code above?

13
  • try abc.prototype = ... or va.b = ... Commented Jun 20, 2015 at 16:28
  • 1
    What is your actual question? Commented Jun 20, 2015 at 16:28
  • @TaoP.R. my console.log(va.b) is undefined, why? Commented Jun 20, 2015 at 16:28
  • 1
    Did you mean "property", not "prototype"? It should just be va.b = 'Hey b is added'; Commented Jun 20, 2015 at 16:28
  • 1
    FYI, va.prototype is just a normal property which happens to have the name "prototype". It has nothing to do with prototypal inheritance if that's what you meant. Commented Jun 20, 2015 at 17:21

4 Answers 4

2

It seems like you really just want to add a property to va:

va.b = "Hey b is added";

If, though, you want to augment the prototype that va already has, you do that through a reference to va's prototype object, which you can get in several ways:

  • Given your code above, via abc.prototype

  • Or on ES5+ browsers, via Object.getPrototypeOf(va)

So for instance:

function Abc() {
  this.a = "Hey this is A";
}

var va = new Abc();
snippet.log(va.a); // "Hey this is A"
snippet.log(va.b); // undefined

Abc.prototype.b = 'Hey b is added';

snippet.log(va.b); // "Hey b is added"
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Note that adding a property to the prototype means that all objects using that prototype will inherit that property:

function Abc() {
  this.a = "Hey this is A";
}

var a1 = new Abc();
var a2 = new Abc();

Abc.prototype.b = 'Hey b is added';

snippet.log(a1.b); // "Hey b is added"
snippet.log(a2.b); // "Hey b is added"
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

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

2 Comments

so I can't actually do a1.prototype.b = "set b", but the correct way is do Abc.prototype.b ="set b" ?
@AaronMusktin: It depends entirely on what end result you want, whether you should modify the instance (vm, in your question) or the prototype (abc.prototype, in your question).
2

You probably just want to add the property to va.

va.b = 'Hey b is added';

Unless you want a b property added to all instances of abc, then you can add it to abc's prototype:

abc.prototype.b = 'Hey b is added';

3 Comments

I thought can add to va variable since it's point to abc object?
va !== abc, instead va instanceof abc
@AaronMusktin va doesn't point to abc, it does inherit from abc.prototype only.
1

Try this:

<script>
    function abc()
    {
        //constructor
        this.a="Hey this is A";
    }
    abc.prototype.b=function()
    {
        //prototype
        this.b='Hey b is added';
    };
    var va=new abc();//Initialization
    va.b();//value for b is assigned to the context
    console.log(va.b);
</script>

Comments are provided..

2 Comments

Now you've created a method that overwrites itself on call. Urghh. If this really was what the OP intended to do, you should at least advise against it.
@Bergi, my intension was to make him understand the execution flow. He is not clear about that..
0

You didn't create the b property, because you never called va.prototype.

function abc(){
 this.a = "Hey this is A";   
}
var va = new abc();
va.prototype = function(){
  this.b = 'Hey b is added';
};
va.b; // undefined
va.prototype();
va.b; // "Hey b is added"

Note that using prototype as the name for your method can be confusing (there already are the prototype of a function and the [[Prototype]] of an object). I recommend choosing another name.

2 Comments

Please also advise against using "prototype" as a name for a method.
@Oriol, that's funny. I bet there's a clearer way to accomplish what the OP is trying for though.

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.