【问题标题】:Javascript Callable and prototype extendable FunctionJavascript 可调用和原型可扩展函数
【发布时间】:2011-06-06 23:26:00
【问题描述】:

基本上,我正在寻找在使用 javascript 原型方法时将方法附加到可执行函数的能力。下面的代码演示了我正在谈论的内容和我正在寻找的功能,但它确实是一个 hack。请注意,我有一个有效的 this 对象来附加变量以及 main 和 init 函数。

function create(){
    var $this = {},
    main = function(){
        prototype.main.apply($this,arguments);
    };
    prototype.init.apply($this,arguments);
    //Add additional prototype methods by brute force, ugly
    for(i in prototype)-function(i){
        main[i]=function(){
            prototype[i].apply($this,arguments);
        }
    }(i);
    return main;
};

var prototype = {
    //called when you create the object
    init:function(text){
        console.log('init');
        this.text = text;
    },
    //called when you call the object
    main:function(){
        console.log('main');
        console.log(this);
    },
    method:function(){
        console.log(this.text);
    }
};

//create returns a function that also has methods
//the below line will call the init method
var fun = create('some variables');
//call main function
fun();
//call methods
fun.method();

恐怕我会遗漏一些明显的东西。

这里的功能与上面相同,但扩展了全局函数原型。

扩展全局属性是不好的做法,所以我正在寻找替代解决方案。

Function.prototype = {
    //called when you create the object
    init:function(text){
        console.log('init');
        this.text = text;
    },
    //called when you call the object
    main:function(){
        console.log('main');
        console.log(this);
    },
    method:function(){
        console.log(this.text);
    }
};

function create(){
    var ret = function(){
        ret.main.call(main);
    };
    ret.init.apply(main,arguments);
    return ret;
};

//create returns a function that also has methods
//the below line will call the init method
var fun = create('some variables');
//call main function
//fun();
//call methods
fun.method();

很明显,您似乎不能使用典型的新对象方法,因为如果您调用 new 则无法返回单独的值。

任何解释或考虑都会很棒!

【问题讨论】:

    标签: javascript function-prototypes


    【解决方案1】:

    您可以将原型函数放入“构造函数”主体中。从技术上讲,这就是您当前正在做的事情,但是明确定义它们而不是使用辅助方法要干净得多。然后,您可以对公共和私有变量和方法使用以下模式进一步简化您的代码:

    function Fun(text) {
        // This is the main function
        var fn = function () {
            return 'main';
        };
    
        // Attach public variables and methods
        fn.publicVariable = 'public';
        fn.publicMethod = function () {
            return text; // text is a "private variable"
        };
    
        // Do whatever initialization
        console.log('init');
    
        // Return the main function     
        return fn;
    }
    
    var fun = Fun('this is some text'); // "init"
    fun() // "main"
    fun.publicMethod() // "this is some text"
    console.log(fun.publicVariable); // "public"
    console.log(fun.text); // undefined
    

    【讨论】:

    • 感谢您的回复!基本上我试图将方法与创建函数分开。我希望其他人成功地扩展到返回元素的原型并引用当前对象的 this。
    • 另外,我试图避免直接扩展元素。我想在不污染全局函数原型命名空间的情况下使用 javascript 的原型功能。直接扩展元素可能会很慢。
    【解决方案2】:

    “JavaScript 原型方法”是指使用Function.prototype 属性来实现继承吗?或者您只是想创建具有初始化程序和附加方法的函数?

    你的例子是后者,所以我假设这就是你要找的。这是否符合您的要求?

    function create(text)
    {
        var main = function()
        {
            console.log('main');
            console.log(this);
        }
        var init = function()
        {
            console.log('init');
            main.text = text;
        }
        main.method = function()
        {
            console.log(main.text);
        }
        init();
        return main;
    }
    //the following line will call init
    var fun = create('some variables');
    //call main
    fun();
    //call methods
    fun.method();
    

    【讨论】:

    • 我试图避免直接扩展元素。我想在不污染全局函数原型命名空间的情况下使用 javascript 的原型功能。直接扩展元素可能会很慢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 2021-01-27
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多