Why does Javascript have privileged functions
After an discussion at work, I still do not have an satisfying answer about the following, in no other programming language we speak of privileged functions.
var kid = function(name)
{
// Private
var idol = "Paris Hilton";
// Privileged
this.get_idol = function()
{
return idol;
};
}
The only logic I could think of is that if you make an public function return an private variable, that function is 'privileged' to do this (because you cannot call it directly).
We can do the same in php but we do not use this fancy name, nor can i recall any other language using this term.
You might even come to thing that if you need an 'privileged' function your whole approach is wrong, since if you need it accessible from the outside, why not make it public directly.
On the other hand the private variable cannot be changed from the outside and therefor it would become protected. but displayed to the outside.
In the end, as an somewhat newcomer to plain javascript, the term is somewhat confusing and imho just a fancy name to make it more confusing, bec开发者_开发知识库ause then in php it would become also like javascript :
class kid
{
// Private
private idol = "Paris Hilton";
// PUBLIC OR PRIVILEGED ?
public function get_idol()
{
echo $this->idol;
}
}
it does the same but is just a public method.
Javascript doesn't have privileged or whatever functions. All it's functions are closures and that's what is in use there, C# got this also and most functional languages.
The ECMA norm for the language is available if you need, the word privileged don't appear once inside of it.
The term seem to come from http://www.crockford.com/javascript/private.html but it's just a simplification of the closure concept to a specific case of their usage targeted i guess to OOP-programmer types.
Javascript could be more powerful design-wise than most OOP languages but you need to approach it with an open mind and by understanding functional concepts. Trying to coerce it to become yet-another-OOP-language would be both a bad idea and a shame.
I've never heard of a privileged function; the correct term is a closure: you define a function in a scope which has access to variables defined in its outer scope. Closures can be used in lots of languages (Javascript, Python, Lisp/Scheme all do, and Java does in part through inner classes that can reach final variables in the closure scope).
Member variables of an object (e.g. this.foo in Java/Javascript or this->foo in PHP) are different.
Yes, the pattern is used to simulate private properties and public methods accessing them.
JavaScript does not really have public/private properties/functions of objects. The private properties is a local variable in the constructor function, which may be accessed by the sub-function ("closure" in javascript terms). And then a reference to the sub-function is added as property to the object.
I don't think the term "privileged function" is commonly used. I have only seen it in "JavaScript for OOP people" tutorials.
The terms public, private, and privileged are not part of the JS language definition. As mentioned elsewhere, the term privileged for JS was popularized by Douglas Crockford.
The point is that JavaScript supports information hiding.
The significant distinction of public versus privileged is that when you define a function as part of a prototype, the function cannot access private members. For example:
kid.prototype.doSomething = function () {
// Cannot access the idol property
};
You correctly infer that the purpose is to make the variable idol
private. It can only be read from the outside, not modified.
But your code in PHP is not equivalent because the JS kid
is a function that has stuff on it. A more common idiom would be something like:
var kid = function() { return {}; }();
The key here is that now kid is an object defined inside the function, so all the private state of kid
can be hidden and only the attached public methods are usable.
var kid = function(name)
{
// Private
var aVal = 1;
// Privileged
this.get_idol = function()
{
return aVal++;
};
}
var myKid= new kid();
//returns:1
myKid.get_idol();
//returns: 2
myKid.get_idol();
//returns: 3
myKid.get_idol();
May be someone has used the word privileged , just to make the point that var aVal = 1;
exist even after execution of the anonymous function and can be accessed from the get_idol
.
I myself have never heard of the word privileged, you should actually look for closure and anonymous function
精彩评论