2011-06-06 59 views
2

基本上我正在寻找在使用javascript原型方法时将方法附加到可执行函数的功能。下面的代码演示了我正在谈论的和我正在寻找的功能,但它确实是一种破解。注意我有一个有效的这个对象来连接变量以及main和init函数。Javascript Callable和prototype extendable函数

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你不能返回一个单独的值。

任何解释或考虑将是伟大的!

回答

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 
+0

感谢您的回应!基本上我尝试从create函数中分离出这些方法。我希望其他人成功扩展到返回元素的原型并引用当前对象的原型。 – William 2011-06-07 00:27:33

+0

另外,我试图避免直接扩展元素。我想在不污染全局函数原型命名空间的情况下使用javascript的原型功能。直接扩展元素可能会很慢。 – William 2011-06-07 00:36:29

0

通过“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(); 
+0

我试图避免直接扩展元素。我想在不污染全局函数原型命名空间的情况下使用javascript的原型功能。直接扩展元素可能会很慢。 – William 2011-06-07 00:36:14