Which is better Javascript object pattern [closed]
Which is a better Javascript object pattern ...
function dog(name) {
this.name = name;
this.getName = function() {
return this.name;
};
};
OR
function cat(name) {
this.name = name;
};
cat.prototype.getName = function() {
return this.name;
};
AND WHY?
----- edits
Does one or the other use more memory?
Is one more or less "CPU" intensive than the other?
Which is more maintainable?
Which is more scalable?开发者_如何学JAVA
Which is more readable?
Preferences aside, the second example is the "right" one. In the first one, you're creating a new getName
function for every object. In 2, all objects created with this constructor will share the prototype/getName
. Change it in one place and it will change for every instance.
On special occasions (like complex inheritance chains) you might need to use the first, but be aware of it's drawbacks.
This blog post might help you understand prototypal inheritance better.
Just a personal opinion here... but I prefer the "dog" style notation for long-term maintainability.
All "dog" code stays contained within the dog definition. Imagine it's 5 years later and "the next guy" is instructed to remove the hasTail property. No matter how many other objects/properties/etc. were defined between now and then, he'll find it within the dog definition.
For comparison... imagine it's 5 years later and "the next guy" is instructed to remove the hasTail property from "cat". He has to scan/grep/ctl+f the animals.js file for hasTail and hope he removes the correct one.
I personally prefer wrapping the function definitions within the object (e.g the dog notation), as it's more like a Java/PHP "class". However using the dog notation is less memory efficient as the functions are copied each time a new instance is created (see this article)
I use the cat
notation because it doesn't introduce unnecessary closures, which can be a little inefficient.
However, I've never tested exactly how inefficient they are, and it might not make that much of a difference in this age of hyper-optimized JavaScript engines.
I prefer the cat method that uses the prototype.
- I'd rather not have all my methods indented one more level inside the contructor.
- I'd rather have the constructor code be just the constructor code rather than lots of other functions - I think it makes it easier to see what you're looking for.
- I'd rather use the prototype so all the methods don't have to be assigned every time the constructor is run.
- It also seems to me like this is why the whole prototype exists, so you can put things on it that future instantiations will inherit automatically without having to run code to assign them.
- Also, if you ever want to do a mix-in, you can get all the methods from the prototype without having to construct one of the objects.
- When you start subclassing, I find the prototype scheme much more understandable.
- I find it easier to search for method definitions because they all include
.prototype.
.
精彩评论