使用对象 B 的 JAVAScript 对象 A 看不到它的功能

JScript oject A using object B don't see it's functions

有两个 Javascript 文件,比方说 AB

一个包含在另一个中 - B 使用来自 A 文件的对象。

在我的 debug 函数中,我首先创建了 AConfig 对象, 然后我创建 BElementBuilder,它使用 ABuilderAConfig 对象;

目前一切正常。

但是,当我调用 bElement.getBString 时,它进入内部并在 me.aBuilder.getAString(Aconfig); 上失败并出现错误

object doesn't support this property or method

为什么会这样?

这里是A

var AConfig = (function() {
    function AConfig(name, flag){
        this.name = (name) ? name : -1;
        this.flag = (flag) ? true : false;

        return this;
    }   

    return AConfig;     
})();

var ABuilder = (function() {
    function ABuilder(config){
        this.config = (config) ? config : new AConfig();

        return this;
    };

    ABuilder.prototype = {
        getAString: function(configObj){
            var me = this,
                config = (configObj) ? configObj : me.config,
                name = me.name,
                flag = me.flag;

            return 'A name is' + name + 'flag =' + flag;
        }
    }

    return ABuilder;
});

这是B

!INC aFile.A

   var BElementBuilder = (function() {
        function BElementBuilder(aConfig, bName){
            this.aConfig = (aConfig) ? aConfig : new AConfig();
            this.bName = (bName) ? bName : "B";
            this.aBuilder = new ABuilder();

            return this;
        };

        BElementBuilder.prototype = {
            getBString: function(configObj){
                var me = this,
                    Aconfig = (configObj) ? configObj : me.aConfig,
                    name = me.bName;
                //and here it fails 
                Aconfig = me.aBuilder.getAString(Aconfig);

                return 'B has config of' + Aconfig;
            }
        }

        return BElementBuilder;
    })();

    function debug(){
        var aConfig = new AConfig("AAA", true);
        var bElement = new BElementBuilder(aConfig);

        var t = bElement.getBString(aConfig);
    };

    debug();

P.S。 如果有什么不同,那就是 JScript

您忘记了 ABuilder 模块上的 IIFE 调用。这样,ABuilder 就不是 class 构造函数,而是 return 新构造函数的模块工厂函数。当您将其称为 new ABuilder(); 时,它将 return 这样一个构造函数分配给 bElement.aBuilder,而不是您期望的实例。