function A(){};
A.prototype.a=function(){
// any other treatment
// Calling A.prototype._b
This._b();
};
A.prototype._b = function(){};
A.prototype._b
is used only in A.prototype.a
and assumes modules that are treated as require('A')
in Node or Browserify.(Omit module.exports
)
In this case, how would you prefer to write A.prototype._b
I personally feel uncomfortable with the above code.
and so on.If you want to solve them,
ex1:Private, if set to a function
function A(){};
var_b = function();
A.prototype.a=function(){
_b();
};
codes such as and
ex2—If you believe that solid writing is acceptable
function A(){};
// var_b = function();
A.prototype.a=function(){
// Write the same process as _b() here.
};
The code comes to mind.
However,
var_b=func~
is independent and uncomfortableA._b=func~
?I don't know what to do like this.
It's no use thinking about it myself, so after all, could you tell me how to write A.prototype._b
is best in this case?
JavaScript is not that private, but I would like to know the preferred form for a general implementation.
Thank you for your cooperation.
javascript
I'm not that familiar with JavaScript, but I wrote it down.A strategy to limit the scope and make it look like that
var A=function(){}
(function(A){
varb=function(obj){console.log('name='+obj.name);};
A.prototype.a = function() {b(this);}
}) (A);
varb = new A();
b.name = 'fkm';
b.a(); // name = fkm output to log
I would like to list two things related to how to write modules.
It's probably a common form.The basic form of the module itself is
(function(){
This.moduleA=function(){};
}());
Or
(function(){
This.moduleB={};
}());
Or
var moduleC=(function(){
return {};
}());
and so on.
Below is a sample description of the initialization that takes the moduleC
pattern and includes a private method.(I prefer _
after the variable name to be private variable naming convention
as shown in )
var moduleC=(function(){
varmodule={};
// PRIVATE
function b_(){
};
// PUBLIC
module.a=function(){
b_();
};
return module;
}());
For general module writing, Eloquent JavaScript: Chapter 10-Modules may be helpful.
I think the following is not common.In the following example, the private method is not hidden.However, I personally prefer this one because it has good performance and can analyze the source code.
(function(){
/**
* @public
* @ constructor
*/
A = function(){};
/** @public*/
A.prototype.a=function(){
// ...
This.b_();
};
/** @private*/
A.prototype.b_=function(){
console.log(this)
};
}());
From this point of view, I felt that the only thing missing was the description of jsdoc.
"This one is ""b is only used with method a!"""
var A=function(){}
A.prototype.a=(function(){
varb=function(obj){console.log('name='+obj.name);};
return function() {b(this);}
})();
varc = new A();
c.name = 'fkm';
c.a(); // name = fkm output to log
© 2024 OneMinuteCode. All rights reserved.