简体   繁体   English

Javascript:使用原型函数发布

[英]Javascript: Issue using prototype functions

function test() {
 this.list = {};
 document.getElementById("test").innerHTML = "";
 this.list['test1']= new mutation("test1", 2, {"damage":{"freq":2, "size":"d6"}});
 this.list['test2']= new mutation("test2", 1, {"range":"long"});
 this.list['test1'].level=5;
 alert("test running");
 this.addOptions();
}
test.prototype.addOptions = function(){
 alert("addOptions Running");
 var node = document.getElementById("select");
 var strName;
 for(strName in this.list) {
  var optionNode = document.createElement('option');
  var textnode = document.createTextNode(this.list[strName].name);
  optionNode.appendChild(textnode);
  node.appendChild(optionNode);
  document.getElementById("test").appendChild(this.list[strName].display());
 }
}

I have been hitting my head against the wall for about an hour now, to no avail. 我已经把头撞在墙上约一个小时了,但无济于事。

I did this exact thing in another file and it not only worked and is still working, but is actually called by this file in the mutations() definition. 我在另一个文件中做了这个确切的事情,它不仅起作用而且仍在起作用,但实际上是在mutations()定义中被该文件调用的。

I am getting Uncaught "TypeError: undefined is not a function" as the error on my this.addOptions() line. 我的this.addOptions()行上出现错误,导致Uncaught "TypeError: undefined is not a function" All of the code was working before I refactored it into a new file and started trying to apply the prototyping for my next step, which will be to make the display update based on a list in the "select" box. 在我将其重构为新文件并开始尝试将原型应用于下一步(即根据“选择”框中的列表进行显示更新)之前,所有代码均在工作。 again, huge work in progress, but this is halting progress rather effectively. 同样,大量工作仍在进行中,但这相当有效地阻止了进展。

edit: This code works just fine 编辑:此代码工作正常

function mutation(nam, freq, opts) {
 this.opts=opts;
 this.name=nam;
 this.nameText = document.createTextNode(nam+": ");
 this.frequency=freq;
 this.level=0;
 this.upd();
}

mutation.prototype.display = function(){
 this.upd();
 var node=document.createElement('span');
 var ret = document.createElement('br');
 node.appendChild(this.nameText);
 node.appendChild(this.damage);
 node.appendChild(this.range);
 node.appendChild(ret);
 return node;
};

mutation.prototype.upd = function(){
 if(this.opts['damage'] || this.opts['Damage']) {
  var dmg = calcDamage(this.level, this.opts['damage']['freq'], this.opts['damage']['size']);
  this.damage = document.createTextNode(dmg+" ");
 } else {
  this.damage = document.createTextNode("");
 }
 if(this.opts['range'] || this.opts['Range']) {
  var rng=rangeFunction(this.level,this.opts['range']);
  this.range = document.createTextNode(rng+"ft. ");
 } else {
  this.range = document.createTextNode("");
 }
};

Seems like you're calling test() constructor before it's actual definition, and its definition gets "hoisted", ie moved to the top of the code. 好像您在实际定义之前调用了test()构造函数,并且其定义被“提升”,即移至代码的顶部。 So the constructor (test()) works fine, BUT the prototype method is still not defined. 因此,构造函数(test())可以正常工作,但原型方法仍未定义。 However, second part with assigning to the prototype is still not defined at that pointed. 但是,目前仍未定义分配给原型的第二部分。 Please check your code order. 请检查您的代码顺序。

Another example with error: 另一个有错误的例子:

a = new A(); // created OK
a.foo(); // error: foo is not a function
A = function() {};
A.prototype.foo = function() { return 'bar'; }

And this one works: 而这一作品:

A = function() {};
A.prototype.foo = function() { return 'bar'; }
a = new A(); // created OK
a.foo(); // returns 'bar'

You would get the error that you describe if you called "test()" instead of "new test()". 如果调用“ test()”而不是“ new test()”,则会得到描述的错误。 If you forgot the "new" then 'this' would not extend test.prototype and then this.addOptions() wouldn't exist. 如果您忘记了“新”,那么“ this”将不会扩展test.prototype,那么this.addOptions()将不存在。 Calling with 'new' creates a 'this' which extends the prototype and has a reference to addOptions. 用'new'调用会创建一个'this',它扩展了原型并引用了addOptions。

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

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