0

Im trying to set up a variable and pass it constructor function essentailly like an anonymous type in c# but javascript doesnt seem to like it...

var aPerson = function Person(){

};


$(document).ready(function(){

  Person.prototype.age = 22;

  window.alert(aPerson .age);

});

Why cant I do this?

4
  • 3
    This code looks awfully familiar... Commented Jun 15, 2011 at 14:35
  • 1
    possible duplicate of Why doesn't this edit to a prototype work? Commented Jun 15, 2011 at 14:36
  • 1
    Oh please, don't ask the same question twice! Commented Jun 15, 2011 at 14:37
  • sorry guys the question is actually different but similar...not sure if I should just roll into one question but that sort of doesnt get to the point.... Commented Jun 15, 2011 at 14:48

3 Answers 3

2

Person is only declared as part of your aPerson variable, but it needs to be defined explicitly (e.g. function Person(){}) before it can be used for prototypical inheritance. You need something more like this:

// Create a Person function object
function Person() {

};

// Modify that function's prototype
Person.prototype.age = 22;

$(document).ready(function() {
  // aPerson.__proto__ = Person.prototype
  var aPerson = new Person();

  // JS checks whether aPerson has age already set, if not, it checks
  // aPerson's prototype -- in which case it's given the value 22
  alert(aPerson.age);
});

Here's the deal: the property prototype works together with new by copying the prototype reference to the object (you can see what that entails by running console.dir(aPerson) in Chrome console, for example). JavaScript checks the original object itself first, then prototype to see whether a function or property exists. That means you can change the reference prototype age later and see the changes reflected in the original object. Also, you can declare your own age in the original object itself and have that override the prototype.

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

Comments

2

I'm quite certain you have to reference your function using aPerson rather than Person (externally). Internally, you'd reference it using Person.

Alternatively, you could do this -

function Person() {

}

var aPerson = new Person();

$(function() {
  Person.prototype.age = 22;
  alert(aPerson.age) //22
});

Comments

0

because aPerson doesn't have an age. You will have to call new for it to work.

var aPerson = function Person(){

};


$(document).ready(function(){

  Person.prototype.age = 22;
  bPerson = new Person();


  window.alert(bPerson.age);

});

9 Comments

but ive added age to the prototype of the Person class doesnt that mean that all objects will get the age?
Mind that bPerson is a global variable.
This doesn't work because Person is not defined.
@Bryan: Correct, because of the named function expression: Person is (or at least should be) only available inside of the function itself.
@Pete Right. You can actually set aPerson to inherit from Person using your format, but you need to define Person first. The above would work if you just put function Person(){} at the beginning.
|

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.