How should we write down the internal methods that are called only once?

Asked 2 years ago, Updated 2 years ago, 18 views

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.

  • Public
  • The method is called only once

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 uncomfortable
  • If
  • , what about A._b=func~?
  • If you don't agree with either of them, is it okay to say, "If you call me only once, I'll write everything down?"

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

2022-09-29 22:13

3 Answers

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


2022-09-29 22:13

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.


2022-09-29 22:13

"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


2022-09-29 22:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.