简体   繁体   English

如何在javascript中调用窗口对象的用户定义函数

[英]how to call user-defined function of a window object in javascript

I am trying to call a function from outside which is defined with in a window object, but it throws an error. 我试图从外部调用在窗口对象中定义的函数,但是会引发错误。

    window.vcm = (function() {
    "use strict";
     function red(){
      alert('red');
     }   
   });  
  vcm.red();//Error at this line...

I am new to OOPS in javascript. 我是javascript的OOPS新手。 Can somebody tell me how to call this function here. 有人可以告诉我如何在这里调用此函数。

The value that is vcm does not have a red property. vcm的值没有red属性。

Perhaps you mean this instead, wherein vcm is an object that has a property red that is function you can call: 也许您的意思是相反的,其中vcm是一个具有red属性的对象,可以调用该函数:

window.vcm = {
  red: function(){
    "use strict";
    alert('red');
  }   
};

It's also a possibility (albeit not somewhat you 'd see in practice) for vcm itself to be a function and for it to have a red property that is a function: vcm本身也可能是一个函数, 并且具有red属性即函数这也是可能的(尽管实际上您不会看到):

window.vcm = (function() {
  "use strict";
   var vcm = function() { alert("vcm"); }
   vcm.red = function() { alert('vcm.red'); };
   return vcm;
 })();

vcm();     // "vcm"
vcm.red(); // "vcm.red"

There are two approaches. 有两种方法。

Approach 1 : window.vcm = { red: function (){ "use strict"; alert('red'); } }; vcm.red(); 方法1: window.vcm = { red: function (){ "use strict"; alert('red'); } }; vcm.red(); window.vcm = { red: function (){ "use strict"; alert('red'); } }; vcm.red();

Approach 2 : 方法2:

window.vcm = (function() {
    "use strict";
   this.red = function(){
      alert('red');
    }   
});
var x = new vcm();
x.red(); 

red only exist inside the function you assigned to window.vcm and also only when the function is executed. red仅存在于分配给window.vcm的函数中,并且仅在执行该函数时存在。 Furthermore, functions don't have a property red . 此外,函数没有属性red

Consider this simpler example: 考虑以下简单示例:

function foo() {
    function bar() {}
}

bar(); // will throw an error

Calling bar will throw an error because bar is not defined in the scope where it is called. 调用bar将引发错误,因为未在调用它的作用域中定义bar

It seems you want to assign an object to window.vcm , which has a property red : 似乎您想将一个对象分配给具有red属性的window.vcm

window.vcm =  {
    red: function (){
        "use strict";
        alert('red');
    }
};

Here I am using an object literal to create an object with the property red . 在这里,我使用对象文字来创建具有属性red的对象。

More information: 更多信息:

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

相关问题 如何使用用户定义的相等函数在Javascript中创建一组用户定义的对象? - How to create a Set of user-defined objects in Javascript with a user-defined equality function? 如何避免在用户定义的函数中使用JavaScript eval() - How to avoid using JavaScript eval() in user-defined function 用Javascript获取用户定义的对象 - Get a user-defined object in Javascript 在jQuery对象上调用用户定义的函数 - Calling user-defined function on jQuery object 在javascript中将复杂的,嵌套的,用户定义的对象序列化为字符串 - Serializing a complex, nested, user-defined object into a string in javascript 如何将用户定义的属性传递给可能不存在的Javascript对象? - How to pass user-defined properties to Javascript object that may not exist yet? 是否可以在sqlite中创建一个javascript用户定义的函数 - Is it possible to create a javascript User-defined function in sqlite 如何在打字稿或JavaScript中将用户定义的日期时间转换为毫秒? - How to convert user-defined datetime to milliseconds in typescript or javascript? 如何在 Javascript 中设置自定义时间并增加 2 小时? - How to set user-defined time in Javascript and add 2 hours to it? 如何在Javascript中JSON.stringify用户定义的类? - How to JSON.stringify a user-defined class in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM