I have an object in Javascript like
this:
var Person = {name: "John",
age:37}
I
would like that the name will be statically accessible every time I make a new instance
of the class while the age will always be
new.
So for
example:
var per1 = new
Person();
Person.name = "Anton";
Person.age =
11;
per1.Name //Anton
per2.Name
//11
var per2 = new Person();
per1.age
//Anton
per2.age //37
????
Is
there a way to make it so that it works that way?
Answer
In order to make a propery static in
javascript you can use
prototype:
Person.prototype.name =
"Anton"
UPDATED:
You
might want to use it this
way:
var Person =
function(age){
this.age=age;
}
Person.prototype.name =
"Anton"
var per1 = new
Person(12);
console.log(per1);
No comments:
Post a Comment