質問

As I know, in JavaScript there is no good solution to make private member. Solution described here is not efficient, because private members become parts of objects, not prototypes, and hence require more memory.
So, I decided just use Python practice - to mark private stuff with leading underscore, letting other to know that makred property or method is not intended to be used from outside.
But there is well-known code quality tool - JSLint, and it suggests to not use leading or trailing underscores.
What is the rationale behind this? Is this just code style suggestion or underscores can lead to more serious problems? If its just code style convention in JS community, how strong it is?

役に立ちましたか?

解決

JSLint is a good tool, but it expresses opinions about coding practices that are in the style of its authors. Read about what those preferences are here. There is no harm in the JavaScript parser in using a leading underscore/underbar, the tool is programmed to see this as a bad convention and warn you against using it. If using leading underscores is your preference and it makes sense, use them.

If you don't want to see the warnings in JSLint when using identifiers that begin with an underscore/underbar there is a setting within JSLint to hide these from you. Wrap the code that you don't want to be evaluated like this example and you won't see the warnings:

 /*jslint nomen: true */
 var _gaq = {};
 /*jslint nomen: false */

This is true if you're having code evaluated from a file, but if you're on the JSLint website there is an option to "Tolerate... dangling _ in identifiers" that removes the warning as well.

Please note that doing this may cause issues in how JSHint parses the file. Take a look at this link showing JSLint vs JSHint in relation to that flag. But if you're going to go by the JSLint standard mixing JSHint can cause a bit of confusion.

Private variables don't exist in JavaScript without using closures, but it's not a pattern needed for every project execution. If you want to know more about closures in JavaScript check out Ben Nadel's wonderful blog post and NetTuts+

他のヒント

It's just a code style suggestion. You can use JSHint instead and set-up it following the code style in your project/company. As to me there's nothing bad if you mark private members in this way. The main rule should be to follow the unified convention in the whole project. If this makes your code more readable and maintainable, you're free to follow own convention for current project.

Underscore prefix can be use as convention. But is just a convention.

If the private member is a property of the object's instance the only way is to declare a variable in constructor. The properties of an object are never registred in prototype. If you store a property in proto, his value was share with all other instance. It work like statics in POO.

The prototype is only use to solve an undefined property in the instance.

Exemple:

function O(){}
O.prototype.name = "John";
var o = new O;

// o look like that :
// {
//   __proto__: { name: "John"}
// }
console.log(o.name); // write "John"

o.name = "Tom";
// now :
// {
//   name: "Tom",
//   __poto__: { name: "John" }
// }
console.log(o.name); // write "Tom"

The definition of name on instance not override the prototype value. It only store in the instance before the proto value in cascade resolution.


Sorry for my bad english.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top