简体   繁体   English

如何创建或扩展全局对象

[英]How to create or extend a global object

I'm trying to extend an object from an external JS file that is loaded into a page that may have the same object name present. 我正在尝试从外部JS文件扩展对象,该文件已加载到可能具有相同对象名的页面中。

I already have an object in the global scope: 我已经有一个全局范围内的对象:

window.Qo = {}
window.Qo.myvar = 1

I need to include a JS file that will extend this if it does exists, or create it if not. 我需要包含一个JS文件,如果存在的话将对其进行扩展,否则将对其进行扩展。

  (function(Qo) {
    Qo.prototype.var2 = 2

    function secretFn(value) {
      return value
    }

    Qo.prototype.public = function(value) {
      console.log(value)
      return value
    }
    return Qo
  })(window.Qo = window.Qo || {})

The last line, as I understands it, is that the object past to the function is window.Qo if it exists, or an empty object if not. 据我了解,最后一行是该函数之后的对象是window.Qo如果存在),或者是空对象。

But trying to expend the original object, I get errors that the value is not a function, even if I pass a function to myvar . 但是,尝试扩展原始对象时,即使将函数传递给myvar ,我也会得到一个错误,即该值不是函数。

TypeError: (intermediate value)(...) is not a function . TypeError: (intermediate value)(...) is not a function

What am I missing here? 我在这里想念什么?

Also, is there something else to check when including global object on unknown/untrusted third-party website? 此外,在未知/不受信任的第三方网站上包含全局对象时,还需要检查其他内容吗?

EDIT 编辑

I made a code based on the answers below: 我根据以下答案编写了代码:

  ;(function(Qo) {
      // Private variable
      var _bar;

      // Private function
      function _atob(value) {
        if(!typeof value === 'undefined')
        return atob(value)
      }
      // Public functions
      Qo.prototype.getBar = function() {
          return _bar;
      };
      Qo.prototype.setBar = function(bar) {
          _bar = bar;
      };

      return Qo;
  })(window.Qo = window.Qo || {})

  Qo.setBar('test')

Error: TypeError: Qo.prototype is undefined; can't access its "getBar" property 错误: TypeError: Qo.prototype is undefined; can't access its "getBar" property TypeError: Qo.prototype is undefined; can't access its "getBar" property

Probably you need to put a semicolon right before the code you posted. 可能您需要在发布的代码之前放一个分号。 Since 以来

whateverPrecedingExpression
(function(Qo) {
    Qo.prototype.var2 = 2

    function secretFn(value) {
      return value
    }

    Qo.prototype.public = function(value) {
      console.log(value)
      return value
    }
    return Qo
  })(window.Qo = window.Qo || {})

js will treat whateverPrecedingExpression as a function object which you want to call with your following (...) js将把whateverPrecedingExpression当作您要在其后跟(...)调用的函数对象

so do sth like 所以……

whateverPrecedingExpression
;(function(Qo) {
    Qo.prototype.var2 = 2

    function secretFn(value) {
      return value
    }

    Qo.prototype.public = function(value) {
      console.log(value)
      return value
    }
    return Qo
  })(window.Qo = window.Qo || {})

try below code 尝试下面的代码

(function(Qo = window.Qo || {}) {
    Qo.prototype.var2 = 2

    function secretFn(value) {
      return value
    }

    Qo.prototype.public = function(value) {
      console.log(value)
      return value
    }
    return Qo
  })()

have a look at this article on default parameters. 看看这篇关于默认参数的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM