简体   繁体   English

如何让函数使用RequireJS AMD公开其对象和成员方法

[英]How to have a function expose its objects and member methods using RequireJS AMD

Can someone explain to me the process involved to have a function expose its objects and member methods to another defined object using RequireJS AMD? 有人可以向我解释使用RequireJS AMD让函数将其对象和成员方法暴露给另一个定义对象所涉及的过程吗? For example, I have the following defined in hello.js 例如,我在hello.js中定义了以下内容

define(['jquery','student'], function($,stu){
    var sayHello = function(){
        var post = $('#post').html();
        post.html('Hello, student!');
        post.html(post+stu.name.full());
    };
});

define('student',function(){
    var st = function(){};
    st.name = {
        first: 'Johnny',
        middle: 'Jay',
        last: 'Dabby',
        full: function(){
            return (st.name.first + ' '+ st.name.middle + ' '+st.name.last);
        }
    };
    st.course = {
        math: 'Algebra II',
        science: 'Chemistry',
        social_sci: 'World History',
        foreign_lang: 'French',
        getCourse: function(){
             return st.course.math;
        }
    };
    st.activity = {
        study: '1.5 hrs',
        homework: '0.55 hrs',
        break: '1.0 hr',
        extraCurricula: '0.50 hrs',
        getActivity: function(){
            return st.activity.homework;
        }
    };
    return st;
});

I'm trying to integrate this example or something similar to it into a large application, however, the debugger kept throwing "TypeError", saying stu.name.full is not a function. 我正在尝试将这个示例或类似的东西集成到一个大型应用程序中,但是,调试器不断抛出“TypeError”,说stu.name.full不是函数。 Can some explain what I need to do correct the coding style? 有人可以解释我需要做些什么来纠正编码风格?

You have to decide, either make st a class and create an instance from it, or just make st a namespace Object and use it. 你必须决定,使st成为一个类并从中创建一个实例,或者只是使st成为一个名称空间Object并使用它。

The easiest fix is to replace var st = function(){} with var st = {} . 最简单的解决方法是用var st = function(){}替换var st = function(){} var st = {}

define('student',function(){
    var st = {};
    st.name = {
        first: 'Johnny',
        middle: 'Jay',
        last: 'Dabby',
        full: function(){
            return (st.name.first + ' '+ st.name.middle + ' '+st.name.last);
        }
    };
    st.course = {
        math: 'Algebra II',
        science: 'Chemistry',
        social_sci: 'World History',
        foreign_lang: 'French',
        getCourse: function(){
             return st.course.math;
        }
    };
    st.activity = {
        study: '1.5 hrs',
        homework: '0.55 hrs',
        break: '1.0 hr',
        extraCurricula: '0.50 hrs',
        getActivity: function(){
            return st.activity.homework;
        }
    };
    return st;
});
  1. In hello.js, you are not returning any thing from the module. 在hello.js中,您不会从模块返回任何内容。 The whole purpose of the module is to reuse the module in other part of the application. 该模块的整个目的是在应用程序的其他部分重用该模块。 So, i would suggest to return the Object which defines the Module. 所以,我建议返回定义模块的Object。 Something like.. 就像是..

Hello.js Hello.js

define(['jquery','student'], function($,stu){
        var sayHello = function(){
            var post = $('#post').html();
            post.html('Hello, student!');
            post.html(post+stu.name.full());
        };
      return { 
             publicHello : sayHello
         };
    });
  1. In your Student Module, you are defining the properties on the student function and not on the student Object. Student模块中,您要定义student function的属性,而不是student对象。 So, you need to first define an empty object with statement like var st = {}; 因此,您需要首先使用var st = {};等语句定义一个空对象var st = {}; and then add properties which you want to expose from this module. 然后添加要从此模块公开的属性。 At the end, you return the st Object literal which is what this module returns. 最后,返回st对象文字,这是该模块返回的内容。 Additionally, note that name is an internal property in the function and return the functions name. 另外,请注意namefunction的内部属性并返回函数名称。

Student.js Student.js

define('student',function(){
    var st = {};
    st.name = {
        first: 'Johnny',
        middle: 'Jay',
        last: 'Dabby',
        full: function(){
            return (st.name.first + ' '+ st.name.middle + ' '+st.name.last);
        }
    };
    st.course = {
        math: 'Algebra II',
        science: 'Chemistry',
        social_sci: 'World History',
        foreign_lang: 'French',
        getCourse: function(){
             return st.course.math;
        }
    };
    st.activity = {
        study: '1.5 hrs',
        homework: '0.55 hrs',
        break: '1.0 hr',
        extraCurricula: '0.50 hrs',
        getActivity: function(){
            return st.activity.homework;
        }
    };
    return st;
    });

Why are you getting the error "TypeError", saying stu.name.full is not a function ? 你为什么得到错误“TypeError”,说stu.name.full不是函数?

When you JS engine runs the code statement var st = function(){}; 当JS引擎运行代码语句var st = function(){}; it creates a st function. 它创建了一个st函数。 Every functions gets certain predefined properties from function prototype like name , length , arguments . 每个functions都从函数原型中获取某些预定义属性,如namelengtharguments

When you try to create the name property like as shown below, it is trying to override the name property of the function with a new Object literal. 当您尝试创建如下所示的name属性时,它尝试使用新的Object literal覆盖函数的name属性。 However, the function name property is not writable, hence it can't be changed. 但是,函数name属性不可写,因此无法更改。 So, basically, the below code is effectively not doing anything and still the st.name will have the value of st , even after executing the below statement. 所以,基本上,下面的代码实际上没有做任何事情,即使在执行下面的语句之后, st.name仍然具有st的值。

Now, when you use the student module in the Hello.js, the exported module points to a function st and the st.name has a value st . 现在,当您在Hello.js中使用student模块时,导出的模块指向函数stst.name的值为st Using the code like stu.name.full() is actually calling the function which doesn't exists and there is no property named full on the name . 使用像stu.name.full()类的代码实际上是调用了不存在的函数,并且name上没有名为full的属性。 Hence you get the Type error . 因此,您会收到Type error

st.name = {
        first: 'Johnny',
        middle: 'Jay',
        last: 'Dabby',
        full: function(){
            return (st.name.first + ' '+ st.name.middle + ' '+st.name.last);
        }
    };

It is suggested to use the strict mode using "use strict" to allow JS engine to throw an error when any invalid operation is specified. 建议使用严格模式使用“use strict”允许JS引擎在指定任何无效操作时抛出错误。

 "use strict"; var st = function(){}; st.name = {}; 

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

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