最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oop - JavaScript: Public, Private, Protected - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Nov 12, 2013 at 0:19 codelove asked Nov 8, 2013 at 20:58 codelovecodelove 2,0165 gold badges27 silver badges37 bronze badges 3
  • 1 They aren't used right now, they are just already reserved for future use. – Ingo Bürk Commented Nov 8, 2013 at 20:59
  • 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. – Waleed Khan Commented Nov 8, 2013 at 21:00
  • let is used in newer Mozilla implementations. – Teemu Commented Nov 8, 2013 at 21:28
Add a comment  | 

5 Answers 5

Reset to default 11

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

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.

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...

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--;
        }
    }
});

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.

发布评论

评论列表(0)

  1. 暂无评论