Hallo,
protected geht gar nicht mehr.
Hm, kommt das nicht wirklich darauf an, wo man die Variablen definiert?
myObj = {
method1 : function () {
var myPrivate = "privat1";
return myPrivate;
}
}
myFunction = function (callback) {
alert(callback());
}
myFunction(myObj.method1);function object(o) {
function F() {}
F.prototype = o;
return new F();
}
var myExtendedObj = object(myObj);
myFunction(myExtendedObj.method1);
>
Besser so:
~~~javascript
myObj = {
method1 : function () {
var myPrivate = "privat1";
return myPrivate;
}
}
myFunction = function (callback) {
alert(callback());
}
myFunction(myObj.method1);
/*
Crockford 2008-04-07 (http://javascript.crockford.com/prototypal.html):
"The problem with the object function is that it is global, and globals are clearly problematic.
The problem with Object.prototype.begetObject is that it trips up incompetent programs,
and it can produce unexpected results when begetObject is overridden.
So I now prefer this formulation:"
*/
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
var myExtendedObj = Object.create(myObj);
myFunction(myExtendedObj.method1);
"For convenience, we can create functions which will call the object function for us, and provide other customizations such as augmenting the new objects with privileged functions. I sometimes call these maker functions. If we have a maker function that calls another maker function instead of calling the object function, then we have a parasitic inheritance pattern.
I have found that by using these tools, coupled with JavaScript's lambdas and object quasi-literals, I can write well-structured programs that are large, complex, and efficient. The classical object model is by far the most popular today, but I think that the prototypal object model is more capable and offers more expressive power.
Learning these new patterns also made me a better classical programmer. Insights from the dynamic world can have application in the static."
Gruß
jobo