JavaScript: Public, Private, Protected
Asked Answered
A

5

8

I always thought "Public", "Private", and "Protected" keywords were for PHP or Java, not JavaScript but just found out these are reserved words.

Can anyone give me an example of how or when to use these particular reserved words because I have never seen or used them in JavaScript but would like to know when to use them.

Thank you

Arnettearney answered 8/11, 2013 at 20:58 Comment(3)
They aren't used right now, they are just already reserved for future use.Fijian
I don't know about these keywords in particular but I do know that the ECMA spec reserves certain keywords in case of future use. It's possible that these are some such keywords.Mocha
let is used in newer Mozilla implementations.Connaught
T
11

These are reserved for possible future use. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words

Trek answered 8/11, 2013 at 20:59 Comment(0)
P
3

From this source:

"The following are reserved as future keywords by the ECMAScript..."

In other words, they do not have a function at the moment.

Polybasite answered 8/11, 2013 at 20:59 Comment(0)
A
2

The ECMAScript 5 standard mentions the terms "public", "private", and "protected" exactly once each (excluding annexes and copyright notices) in 7.6.1.2:

The following tokens are also considered to be FutureReservedWords when they occur within strict mode code (see 10.1.1).

implements let private public yield

interface package protected static

That is to say, they not currently features of the language, but those identifiers are disallowed in strict mode to make way for their future use.

Note that ES5 forbids these words in strict mode only, but ECMSAcript 3 forbids them at all:

7.5.3 Future Reserved Words

The following words are used as keywords in proposed extensions and are therefore reserved to allow for the possibility of future adoption of those extensions...

Ashwell answered 8/11, 2013 at 21:4 Comment(0)
E
0

https://github.com/nooning/JSClass/ Support private, protected, public, friendly, static virtual

Class(
{
    Namespace:ABC,
    Name:"ClassL3",
    Bases:{"ClassL2":{Namespace:ABC,Policy:Class.Policy.Public}},
    Private:{
        Leg:2
    },
    Protected:{
        TestProtected:0,
        fight:function(){
            console.log("ClassL3::fight "+this.constructor["."].Name);
            this.Leg--;
        }
    }
});
Eckert answered 11/12, 2013 at 6:48 Comment(0)
M
0

You may use ZNOW if you like. http://icarusso.github.io/ZNOW/private.html

private, protected and public are reserved words. Although ECMASCRIPT 6, the next JavaScript version, support class. It seems not all encapsulation levels are supported. Moreover, the use of it seems a bit verbose and wont be available in any major browsers yet. http://wiki.ecmascript.org/doku.php?id=harmony:classes

I guess different encapsulation levels can increase code readability and maintainability. Especially when developing complicated programs and collaborating with other programmers. So I designed this framework. :-)

It uses name prefix to declare different encapsulation levels: _ is private and $ is protected. Example:

var ClassA=Class({
    init:function(a){ //constructor
        this._a=a;
    },
    foo:function(){ //public function
        return this._a;
    },
    _a:false //private attribute
})

var a=new ClassA('a');
console.log(a._a == 'a'); //false
console.log(a.foo() == 'a'); //true

This framework allows you to shift your oo-skills from Java to JavaScript without pain.

Another example is Singleton.

var ClassA=Class({
    'static.getInstance':function(){
        if(!ClassA._instance) ClassA._instance=new ClassA();
        return ClassA._instance;
    },
    'static._instance':false,
    _init:function(){ //private constructor
        console.log('instance created');
    }
})

var a1=ClassA.getInstance(); //>instance created
var a2=ClassA.getInstance();
console.log(a1==a2); //>true

I hope you will find this framework easy to use and easy to read. ZNOW also supports interface, abstracts and const. Here you are the link: http://icarusso.github.io/ZNOW/

Hope you enjoy it.

Mogilev answered 28/12, 2014 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.