向对象添加功能 [英] Add function to object

查看:72
本文介绍了向对象添加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

var PROMO = PROMO || {};

PROMO.Base = (function () {
  var _self = this;

  var Init = function () {
    WireEvents();
  };

  var WireEvents = function () {
   //wire up events
  };

} ());

在同一个文件中我有代码来调用上面的函数

In the same file I have the code to call the above function

我想到达终点,我可以使用以下代码

I am trying to get to an end point where I can use the following code

 $(document).ready(function () {
    PROMO.Base.Init();
 });

这给出了错误

Cannot call method 'Init' of undefined

现在我知道有很多写javascript的方法,但在这种情况下,我希望能够以上面显示的方式调用我的函数,或者至少调用Init方法。

Now I know there are many ways to write javascript, but in this case I want to be able to call my functions, or least the Init method in the way shown above.

推荐答案

var PROMO = PROMO || {};

PROMO.Base = (function () {
  var _self = this;

  var Init = function () {
    WireEvents();
  };

  var WireEvents = function () {
   //wire up events
  };

  var reveal = {
        Init: Init        
  };

  return reveal;

} ());

您需要返回面向公众的功能。查看更新的代码。

You need to return the public facing functions. See updated code.

这篇关于向对象添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆