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!
Is Prototype Synonymous with Static?
No. Properties on the prototype are instance properties. If you attach a method to the prototype of a function then instances created from that function will inherit from that prototype. This means that all instances created from that function will share a single copy of that method on the prototype.
A 'static' property in JavaScript is best represented by a property directly on the constructor function itself:
function Person() {}
Person.count = 0;
Here, count
is a 'static' property on the constructor. You could update that from e.g. within the constructor with Person.count++
;
The example above does not declare the variables in the class so they will be globally scoped
You don't have any variable declarations, only property assignments. Because the prototype method will be called in the context of an instance this
will refer to that instance and therefore the prototype method has access to those instance properties.
The only thing that is 'global' in your example is the person
constructor itself.
JavaScript
is not really an object oriented language so you don't have real encapsulation. You have closures
. Your methods can benefit from a closure and access a variable which is defined only in the scope
of your person function, so you can call them private
. You cannot however access these variables from a method defined on the prototype
because that is no longer the same scope
.
Also prototype is not synonymous with static as explained here.
The main thing about the prototype
is that objects and methods defined on the prototype provide the same instance of those objects and methods to each instance of the object
produced with new
. This means that if a method or object is defined on the prototype
instead of through the function scope it will not have its own instance for each object instance created with new
, which makes creating multiple members more efficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With