For a new project I need to write a JavaScript library and I'm not sure how to structure it. I read many articles and questions here on stackoverflow yesterday.
I like to support public and private functions and classes. So here is my result:
(function(window) {
var Library = Library || {};
// Library namespace
Library = (function() {
function privateFunc() {
return "I'm a private functon in Library.";
}
var privateClass = function(param) {
var _param = param;
}
return {
constructor: Library,
publicFunc: function() {
return "I'm a publicFunc functon in Library.";
}
};
})();
// String namespace
_namespace('String');
Library.String = (function() {
function privateFunc() {
return "I'm a private functon in Library.String.";
}
return {
constructor: Library.String,
publicFunc: function() {
return "I'm a publicFunc functon in Library.String.";
},
publicClass: function(param) {
var _param = param;
开发者_如何学C }
};
})();
// global function
function _namespace(name) {
...
}
// register libary
window.Library= window.$L = Library;
})(window);
Is this a good way to structure a library or are there better ways? And how do I implement private and public functions for my privateClass/publicClass?
Thank you
There is no notion of private / public functions in javascript. By convention functions that start with underscore are considered private.
Just to mess with Raynos (I wrote this years ago):
Private, Public and Static variables
The way that we define variables in our Objects determines what methods our Objects have available to access those variables. In JavaScript, there are five levels of methods and properties when working with OO code.
Private Declared with 'var variableName' or 'function functionName' inside of the object. Can only be accessed by other private or privileged functions.
Public Declared with 'this.variableName' inside of the object. Can be changed by any function or method.
Privileged Declared with 'this.functionName = function(){ ... }' inside of the object. Can be accessed by any function or method and can call reference or change any Private variable.
Prototype Declare with 'Class.prototype.variableName' or 'Class.prototype.functionName'. Functions declared this way will have access to any public or prototype variables. Attempts to change variable created this way will instead create a new public variable on the object and the prototype variable will be unavailable.
Static Declare with 'Class.variableName' or 'Class.functionName'. Can be changed by any function or method. This method is rarely used. To understand these different levels, let's look at an example:
function Cat(name, color){
/*
Constructor: any code in here is run when the object is created
*/
Cat.cats++;
/*
Private variables and functions - may only be accessed by private or privileged functions.
Note that 'name' and 'color', passed into the Class, are already private variables.
*/
var age = 0;
var legs = 4;
function growOlder(){
age++;
}
/*
Public variables - may be accessed publicly or privately
*/
this.weight = 1;
this.length = 5;
/*
Privileged functions - may be accessed publicly or privately
May access Private variables.
Can NOT be changed, only replaced with public versions
*/
this.age = function(){
if(age==0) this.length+=20;
growOlder();
this.weight++;
}
}
/*
Prototyped Functions - may be accessed publicly
*/
Cat.prototype = {
talk: function(){ alert('Meow!'); },
callOver: function(){ alert(this.name+' ignores you'); },
pet: function(){ alert('Pet!'); }
}
/*
Prototyped Variables - may be accessed publicly.
May not be overridden, only replaced with a public version
*/
Cat.prototype.species = 'Cat';
/*
Static variables and functions - may be accessed publicly
*/
Cat.cats = 0;
精彩评论