Javascript";“私人”;与实例属性

Javascript";“私人”;与实例属性,javascript,namespaces,closures,Javascript,Namespaces,Closures,我正在做一些Javascript的研发工作,虽然我已经阅读了《Javascript:The Definitive Guide》(Javascript:The Definitive Guide)和《Javascript面向对象编程》(Javascript Object-Oriented Programming),但我仍然有一些小问题,那就是从基于类的OOP转向基于词汇和对象的OOP 我喜欢模块。名称空间、子类和接口。w00t。以下是我正在玩的游戏: var Classes = { _pro

我正在做一些Javascript的研发工作,虽然我已经阅读了《Javascript:The Definitive Guide》(Javascript:The Definitive Guide)和《Javascript面向对象编程》(Javascript Object-Oriented Programming),但我仍然有一些小问题,那就是从基于类的OOP转向基于词汇和对象的OOP

我喜欢模块。名称空间、子类和接口。w00t。以下是我正在玩的游戏:

var Classes = {
    _proto : {
        whatAreYou : function(){
            return this.name;
        }
    },
    Globe : function(){
        this.name = "Globe"
    },
    Umbrella : new function(){
        this.name = "Umbrella"
    }(),
    Igloo : function(){
        function Igloo(madeOf){
            this.name = "Igloo"
            _material = madeOf;
        }
        // Igloo specific
        Igloo.prototype = {
            getMaterial : function(){
                return _material;
            }
        }
        // the rest
        for(var p in Classes._proto){
            Igloo.prototype[p] = Classes._proto[p]
        }
        return new Igloo(arguments[0]);
    },
    House : function(){
        function House(){
            this.name = "My House"
        }
        House.prototype = Classes._proto
        return new House()
    }
}
Classes.Globe.prototype = Classes._proto
Classes.Umbrella.prototype = Classes._proto

$(document).ready(function(){    
    var globe, umb, igloo, house;

    globe     = new Classes.Globe();
    umb       = Classes.Umbrella;
    igloo     = new Classes.Igloo("Ice");
    house     = new Classes.House();

    var objects = [globe, umb, igloo, house]

    for(var i = 0, len = objects.length; i < len; i++){
        var me = objects[i];
        if("whatAreYou" in me){
            console.log(me.whatAreYou())
        }else{
            console.warn("unavailable")
        }
    }
})
到目前为止还不错。出于语法原因,Globe原型必须在Classes对象之外声明,因为它已经存在(或者实例化了,或者……不知道这个词的“正确”术语),所以伞不能接受,而Igloo有一些闭包可以为您声明它

然而

如果我将其更改为:

var Classes = {
    _proto : {
        whatAreYou : function(){
            return _name;
        }
    },
    Globe : function(){
        _name = "Globe"
    },
    Umbrella : new function(){
        _name = "Umbrella"
    }(),
    Igloo : function(){
        function Igloo(madeOf){
            _name = "Igloo"
            _material = madeOf;
        }
        // Igloo specific
        Igloo.prototype = {
            getMaterial : function(){
                return _material;
            }
        }
        // the rest
        for(var p in Classes._proto){
            Igloo.prototype[p] = Classes._proto[p]
        }
        return new Igloo(arguments[0]);
    },
    House : function(){
        function House(){
            _name = "My House"
        }
        House.prototype = Classes._proto
        return new House()
    }
}
Classes.Globe.prototype = Classes._proto
Classes.Umbrella.prototype = Classes._proto

$(document).ready(function(){    
    var globe, umb, igloo, house;

    globe     = new Classes.Globe();
    umb       = Classes.Umbrella;
    igloo     = new Classes.Igloo("Ice");
    house     = new Classes.House();

    var objects = [globe, umb, igloo, house]

    for(var i = 0, len = objects.length; i < len; i++){
        var me = objects[i];
        if("whatAreYou" in me){
            console.log(me.whatAreYou())
        }else{
            console.warn("unavailable")
        }
    }
})
有人能解释一下吗?显然,
\u name
在每次迭代时都会被覆盖,而不会读取它所附加的对象的属性

这一切似乎有点太冗长了,需要
这个
还有点奇怪


谢谢:)

如果删除关键字“this”,则_名称在“Globe”范围内

查看您的代码

var globe, umb, igloo, house;

globe     = new Classes.Globe();
umb       = Classes.Umbrella;
igloo     = new Classes.Igloo("Ice");
house     = new Classes.House();

最后,house将用“My house”的名称覆盖globe范围中的“_name”值。

您将声明一个全局变量。声明此项后,可以从代码中的任何位置获得它。无论您在哪里请求
\u name
(更接近
窗口。\u name
)您都会收到每次全局请求。在您的情况下,每个函数中的名称都被替换。最后一个功能是House,已设置为“My House”

“私有”(本地)变量的声明必须使用
var
语句

看看这个:

var foo = function( a ) { 
    _bar = a;
    this.showBar = function() { 
       console.log( _bar );
    }
};
var a = new foo(4);   // _bar ( ie window._bar) is set to 4

a.showBar(); //4


var b = new foo(1); // _bar  is set to 1
a.showBar(); //1
b.showBar(); //1

_bar = 5; // window._bar = 5; 
a.showBar();// 5
应该是:

var foo = function( a ) { 

    var _bar = a;
    // _bar is now visibled only from both that function
    // and functions that will create or delegate from this function,
    this.showBar = function() { 
       console.log( _bar );
    };
    this.setBar = function( val ) { 
        _bar = val;
    };
    this.delegateShowBar = function() { 
       return function( ) { 
           console.log( _bar );
       }
    }
};
foo.prototype.whatever = function( ){ 
    //Remember - here don't have access to _bar
};

var a = new foo(4);
a.showBar(); //4

_bar // ReferenceError: _bar is not defined  :)
var b = new foo(1);

a.showBar(); //4
b.showBar(); //1

delegatedShowBar  = a.delegateShowBar();
a.setBar(6);
a.showBar();//6
delegatedShowBar(); // 6

因为你的“private”是全局的。我最初在那里有
var
关键字,但没有把它放回去。这就是为什么你不在凌晨三点问问题;)无论如何,谢谢你的回答,现在有点道理了,同样的解决方案也适用。干杯您可能会对我看到并使用的一些库感兴趣,这些库允许更结构化的类OOP风格的使用,但我仍然需要理解这种语言。不过谢谢,我一直在寻找好的库。本文列出了典型的OO机制。不是肮脏的经典OO模拟。我个人最喜欢的是
var foo = function( a ) { 
    _bar = a;
    this.showBar = function() { 
       console.log( _bar );
    }
};
var a = new foo(4);   // _bar ( ie window._bar) is set to 4

a.showBar(); //4


var b = new foo(1); // _bar  is set to 1
a.showBar(); //1
b.showBar(); //1

_bar = 5; // window._bar = 5; 
a.showBar();// 5
var foo = function( a ) { 

    var _bar = a;
    // _bar is now visibled only from both that function
    // and functions that will create or delegate from this function,
    this.showBar = function() { 
       console.log( _bar );
    };
    this.setBar = function( val ) { 
        _bar = val;
    };
    this.delegateShowBar = function() { 
       return function( ) { 
           console.log( _bar );
       }
    }
};
foo.prototype.whatever = function( ){ 
    //Remember - here don't have access to _bar
};

var a = new foo(4);
a.showBar(); //4

_bar // ReferenceError: _bar is not defined  :)
var b = new foo(1);

a.showBar(); //4
b.showBar(); //1

delegatedShowBar  = a.delegateShowBar();
a.setBar(6);
a.showBar();//6
delegatedShowBar(); // 6