[ACCEPTED]-JavaScript instance functions versus prototype functions-prototype-programming
You can actually add another level of privilege 8 via wrapping the whole thing in a self-executing 7 function:
var MyObj = (function() { // scoping
var privateSharedVar = 'foo';
function privateSharedFunction() {
// has access to privateSharedVar
// may also access publicSharedVar via explicit MyObj.prototype
// can't be called via this
}
function MyObj() { // constructor
var privateInstanceVar = 'bar';
this.publicInstanceVar = 'baz';
function privateInstanceFunction() {
// has access to all vars
// can't be called via this
};
this.publicInstanceMethod = function() {
// has access to all vars
// also known as a privileged method
};
}
MyObj.prototype.publicSharedVar = 'quux';
MyObj.prototype.publicSharedMethod = function() {
// has access to shared and public vars
// canonical way for method creation:
// try to use this as much as possible
};
return MyObj;
})();
Only 'public' properties can be 6 accessed from outside via this
.
For performance 5 reasons, you should avoid what I called 4 'instance' methods: For each of these, a 3 new function object must be created for 2 each MyObject
instance, whereas there's only a single 1 function object per 'shared' method.
Are these correct?
Err. Well, it depends 23 what you want to do. There is no one accepted 22 canonical model for implementing class/instance-style 21 inheritance in JavaScript.
But this:
if (propOne)
Is probably 20 a mistake, in that this.propOne
is a property of the 19 owner object, whereas propOne
by itself is a variable 18 that hasn't been declared (this defaults 17 to a global variable, but is usually a wrongness).
In 16 what cases should one put functions on the 15 prototype (e.g. protoFunc) vs. in the constructor 14 (e.g. publicInstanceFunc)?
A function set 13 on the prototype is the same function shared 12 amongst all objects. The only way it can 11 work out what instance it belongs to is 10 by reading ‘this’ when it is called.
A function 9 set on ‘this’ in the constructor is a new 8 function for every instance of the MyObj
. You 7 can possibly use this as an alternative 6 way to bind to the owner object based on 5 closures instead of ‘this’, which can save 4 writing out function-binding stuff. It's 3 a different style of object-orientation 2 which normally you wouldn't mix with the 1 this-based style.
1) Yes your code is right.
2) I use functions 13 defined within the constructor function 12 when I want to access other members defined 11 privately inside the scope of the constructor 10 function itself, for example, when you want 9 to create privileged methods.
Public functions defined on 8 the constructor function, are more computationally 7 expensive than adding a simple function 6 to the object prototype, but they also give 5 you much more flexibility.
3) Yes, if the 4 property is public you are able to access 3 it by using the this
keyword inside of a prototype 2 extended function, since this
refers to the 1 instance of your object.
Regarding the second point, you extend the 4 prototype so that all the already created 3 objects get the new method.
Also, allows 2 you to add methods to the built-in objects 1 (like adding trim()
to string
).
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.