I am teaching myself JavaScript coming from a OOP background. The book in which I am studying has made me think, "Wow, properties are almost the same as static methods or variables!" If this is the case I would love to implement them more to save some memory, but before I fall in love with them I want to make sure I'm using them correctly.
Is this the case? Is my logic wrong? I am adding some sample code below to use as context for my question. Hopefully it's not oversimplified:
function person(first, age){
this.firstName = first;
this.age = age;
}
person.prototype.sayHello = function(){
return "hi my name is " + this.firstName + " and I am " + age + ".";
};
and thus the constructor function could be called the following way
var me = new person("Dan", 22);
Also, does this break encapsulation? The example above does not declare the variables in the class so they will be globally scoped. I understand the prototype would not be able to see firstName or age if they were declared var firstName and var age.
Do I have to pick one or the other? Can I not use my added prototypes and encapsulation?
Thanks ahead of time!
prototypeis more likeclassrather thanstatic.