JavaScript hiçbir koruma değişkenler/özellikleri vardır. Yine de, özel değişkenler yalnızca prototipinizin "gizli yardımcı programları" olduğunda, olası kapsamı sınıfları aynı kapsamda bildirdiğinizde "özel" değişkenleri yeniden kullanabilirsiniz.
MyNamespace.Person = function Person(params) {
// private variables and functions, individual for each Person instance
var anything, id;
function execute_something() {}
// public properties:
this.name = "";
this.getId = function getId(){
// called a "privileged function", because it has access to private variables
}
}
MyNamespace.American = function(params) {
MyNamespace.Person.call(this, params); // inherit name and getId()
}
(function() { // new scope for
// hidden utility functions and other private things
function foo() { }
function helpJSON() { }
function fromJSON() { }
var bar;
(function(personProto) { // new scope for prototype module (not explicitly needed)
// "private" /static/ variables (and functions, if you want them private)
var personCount = 0;
personProto.clone = function clone() {
return this.constructor(myself); // or something
};
personProto.toJSON = function toJSON() {
// use of helpJSON()
};
personProto.fromJSON = fromJSON; // direct use
})(MyNamespace.Person.prototype);
(function(amiProto) {
// just the same as above, if needed
amiProto.special = function() {
// use foo() and co
};
})(MyNamespace.American.prototype = Object.create(MyNamespace.Person.prototype));
})();
Bu Amerikalının prototip Kişinin prototipten automagicallylar klon(), toJSON() ve fromJSON() işlevlerini devralır anlamına miras JavaScript yoludur. Tabii ki, Bunu gerekir ve, yardımcı programın işlevlerini yeniden verebilecek daha modül benzeri şekilde kullanmak istemiyorsanız Ve özellik Tabii
new MyNamespace.American() instanceof MyNamespace.Person; // true
olduğunu yani sadece onları kopyalamak:
(function() {
// hidden utility functions and other private things
var bar;
var personCount;
function foo() { }
function helpJSON() { }
function fromJSON() { }
function clone() {
return this.constructor(myself); // or something
}
function toJSON() { }
(function(personProto) { // new scope, not really needed
// private variables are useless in here
personProto.clone = clone;
personProto.toJSON = toJSON;
personProto.fromJSON = fromJSON;
})(MyNamespace.Person.prototype);
(function(amiProto) { // new scope, not really needed
// copied from personProto
amiProto.clone = clone;
amiProto.toJSON = toJSON;
amiProto.fromJSON = fromJSON;
// and now the differences
amiProto.special = function() {
// use foo() and co
};
})(MyNamespace.American.prototype);
})();
Veraset ve prototip hakkında daha fazla bilgi edinmek istiyorsanız, http://killdream.github.com/blog/2011/10/understanding-javascript-oop/index.html adresini ziyaret edebilirsiniz. 3. nokta mirasın başladığı yer. –
Ne yazık ki bu link bir 404 hatası veriyor. –
@Programmer_D: http://robotlolita.me/2011/10/09/understanding-javascript-oop.html adresine taşındı. Ve tabii ki de [eski sürümü görebilir] (http://web.archive.org/web/20130127102509/http://killdream.github.com/blog/2011/10/understanding-javascript-oop/index .html) – Bergi