Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为javascript类的属性定义函数_Javascript_Design Patterns - Fatal编程技术网

为javascript类的属性定义函数

为javascript类的属性定义函数,javascript,design-patterns,Javascript,Design Patterns,我有这个Javascript类,想添加一些函数(比如prototype) 此类的属性 function theUploader(virtualField) { var self = this; //self.v = virtualField; self.v = (function(){return self.v; this.clear = function(){self.v = '';}})() self.v.prototype = function clea

我有这个Javascript类,想添加一些函数(比如prototype) 此类的属性

function theUploader(virtualField)
{
     var self = this;
   //self.v = virtualField;
     self.v = (function(){return self.v; this.clear = function(){self.v = '';}})()
     self.v.prototype = function clear() {
       self.v = '';
     }
}
我试过那些台词。 我找不到正确的方法来定义这样的事情.我想这样称呼它

var temp = new theUploader('smart');
temp.v.clear();
有人用jsaon指导我,但仍在努力

self.v.prototype = function clear() {
   self.v = '';
}
应该是

self.v.clear = function() {
    self.v = '';
}
此线路存在以下问题:

self.v = (function(){return self.v; this.clear = function(){self.v = '';}})()
self.v.prototype = function clear() {
…如果将其分为几行,则更为明显:

self.v = (function(){
             return self.v;
             this.clear = function(){
                             self.v = '';
             }
         })()
它是一个立即调用的函数表达式,在第一行返回,因此它永远不会到达
this.clear=…
行。返回的值,
self.v
,此时将是
未定义的
,这意味着您分配该值的
self.v
属性也将是
未定义的
,这意味着在此行:

self.v = (function(){return self.v; this.clear = function(){self.v = '';}})()
self.v.prototype = function clear() {
…您将得到一个错误
TypeError:无法设置未定义的属性“prototype”

考虑到您的
uploader()
函数中存在的混乱,很难准确地说出您想要做什么,但是考虑到您说过您希望能够做到这一点:

var temp = new theUploader('smart');
temp.v.clear();
然后需要创建一个
.v
属性,该属性本身就是一个具有
.clear()
方法的对象,因此:

function theUploader(virtualField)
{
     var self = this;
     self.v = {};                                // create a v property that is an object
     self.v.clear = function(){self.v = '';};    // add a method to v
}
…我会的。或者您可以直接在对象文本中定义
clear
函数:

     self.v = {
        clear : function(){self.v = '';}
     };

(不管怎样,调用
self.v.clear()
实际上会用空字符串覆盖
.v
属性对我来说都没有意义,但如果这是你想要的,那么这就是你可以做的。)

不幸的是,这不起作用。我以前试过。忘了提到onechnanks。这很有帮助。