Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不使用“创建对象”;新";_Javascript - Fatal编程技术网

Javascript 不使用“创建对象”;新";

Javascript 不使用“创建对象”;新";,javascript,Javascript,如何重写此对象,使您不必使用“new”声明它 对这样的事情: var Lang = { get : function(){} }; 只需去掉“新的”,其余的都一样。:) 编辑:来自firebug的复制粘贴示例,“dsa”只是一个对象,“dsaFunc”是一个函数,但您可以对它们执行相同的操作: >>> var dsa = {}; undefined >>> dsa.get = function(a){ return a+1;} function() >

如何重写此对象,使您不必使用“new”声明它

对这样的事情:

var Lang = {
get : function(){}
};
只需去掉“新的”,其余的都一样。:)

编辑:来自firebug的复制粘贴示例,“dsa”只是一个对象,“dsaFunc”是一个函数,但您可以对它们执行相同的操作:

>>> var dsa = {};
undefined
>>> dsa.get = function(a){ return a+1;}
function()
>>> dsa.get(2)
3
>>> var dsaFunc = function(){};
undefined
>>> dsaFunc.get = function(a){ return a+1;}
function()
>>> dsaFunc.get(2)
3

您在问题中为自己的问题编写了解决方案……这是在javascript中创建对象的一种非常有效的方法:

var Lang = {
    get: function(str, trans){
        if(TRANSLATE[str]){
            var str = TRANSLATE[str][LANG];
            if(count_obj(trans) > 0){
                for(var key in trans){
                    str = str.replace('%'+key+'%', trans[key]);
                }
            }
        }

        return str;
    };
};
如果需要私有变量,通常的方法是使用局部变量创建一个函数,该函数返回对象,这些变量封装在闭包中,如下所示:

var Lang = (function() {
    var p1; // private variable
    var p2; // another private variable

    return {
       getP1: function () {
          return p1;
       },
       setP1: function(value) {
          p1 = value;
       }
    };
})();

请注意,创建所需对象的函数会立即执行,并返回包含闭包中捕获的两个私有变量的对象。这些变量将无法从对象外部访问。

用于强制新变量的模式

如前所述,构造函数仍然只是函数,但用new调用。 什么 如果调用构造函数时忘记了new,会发生什么情况?这不会导致 语法或运行时错误,但可能导致逻辑错误和意外行为。 这是因为当您忘记new时,构造函数中的这个将指向全局 对象(在浏览器中,这将指向窗口。)

当你的构造函数有这样的东西时,你调用构造函数 如果没有new,实际上是在创建名为 成员,并可通过window.member或simply member访问。这种行为非常危险 不受欢迎,因为您知道应该始终努力保持全局名称空间 干净

自调用构造函数

解决以前模式的缺点,并提供原型属性 对于实例对象,考虑以下方法。在构造函数中,您 检查这是否是构造函数的实例,如果不是,构造函数将调用 再次强调,这一次使用新的:

function Waffle() {
   if (!(this instanceof Waffle)) {
      return new Waffle();
   }
   this.tastes = "yummy";
}
Waffle.prototype.wantAnother = true;
// testing invocations
var first = new Waffle(),
second = Waffle();
console.log(first.tastes); // "yummy"
console.log(second.tastes); // "yummy"
console.log(first.wantAnother); // true
console.log(second.wantAnother); // true
检查实例的另一种通用方法是与 arguments.callee,而不是硬编码构造函数名称

if (!(this instanceof arguments.callee)) {
   return new arguments.callee();
}
此模式使用这样一个事实:在每个函数中,都会创建一个名为arguments的对象 包含调用函数时传递给函数的所有参数。和论点 具有名为callee的属性,该属性指向被调用的函数。是 请注意,在ES5的严格模式中不允许arguments.callee,因此最好限制 如果您在现有代码中找到任何实例,还可以删除它们


“JavaScript模式,作者Stoyan Stefanov(O'Reilly)。版权所有2010 Yahoo!Inc.,9780596806750。”

但是它不会被声明为一个对象,而是一个函数。从实用的角度来看,JavaScript函数和对象几乎是一样的。@Akos:这不是创建JavaScript对象的方式……请参见用户278064的答案了解原因。下面是一个简单的测试用例,用于测试var f=function(){this.abc=10;};f、 abc将是未定义的,其中就好像您实际使用了var f=new function(){this.abc=10;};f、 abc将如你所料为10。user278064确实有更好的解释。作为旁注:var f2={abc:10};似乎与var f=new function()相同{this.abc=10;};您自己使用var Lang={…}的解决方案有什么问题吗?呵呵。。我不知道是这样的啊。。。但是,如何在Lang对象中拥有私有变量呢?如果要在对象中放置“var private_var='something'”,请更新答案,以包含一个带有私有变量的示例。
// constructor
function Waffle() {
   this.tastes = "yummy";
}
// a new object
var good_morning = new Waffle();
console.log(typeof good_morning); // "object"
console.log(good_morning.tastes); // "yummy"
// antipattern:
// forgotten `new`
var good_morning = Waffle();
console.log(typeof good_morning); // "undefined"
console.log(window.tastes); // "yummy"
function Waffle() {
   if (!(this instanceof Waffle)) {
      return new Waffle();
   }
   this.tastes = "yummy";
}
Waffle.prototype.wantAnother = true;
// testing invocations
var first = new Waffle(),
second = Waffle();
console.log(first.tastes); // "yummy"
console.log(second.tastes); // "yummy"
console.log(first.wantAnother); // true
console.log(second.wantAnother); // true
if (!(this instanceof arguments.callee)) {
   return new arguments.callee();
}