javascript - this.set is not a function -


i have code this:

var ws2812 = {};   ws2812.set = function(r,g,b){     $.get( "/light?r="+r+"&g="+g+"&b="+b, function( data ) {         console.log("light set to:"+"/light?r="+r+"&g="+g+"&b="+b);     }) };  ws2812.speech = function(word){     switch(word){         case "czerwone":             this.set(255,0,0);         break;         case "zielone":             this.set(0,255,0);             break;         case "niebieskie":             this.set(0,0,255);             break;         case "białe":             this.set(255,255,255);             break;         default:             this.set(0,0,0);             break;      } } 

when running ws2812.speech(""); inside console, works. however, when paired annyang library, this:

uncaught typeerror: this.set not function  ws2812.speech @ script.js:29   b.annyang.init.d.onresult @ annyang.min.js:6 

what's wrong?

[edit]

the command added this:

annyang.addcommands({"ustaw *term światło":ws2812.speech}); 

specifically, inside annyang, line fails:

f[h].callback.apply(this,m) 

is replacing this ws2812 way work around this?

ws2812.speec defined static function. this keyword inside refers (function scope), not object ws2812 want.

to fix it, either of these quick choices can made:


choice#1 > call static function ws2812.set properly

so code becomes:

ws2812.speech = function(word){     switch(word){     case "czerwone":         ws2812.set(255,0,0);     break;     case "zielone":         ws2812.set(0,255,0);         break;     case "niebieskie":         ws2812.set(0,0,255);         break;     case "białe":         ws2812.set(255,255,255);         break;     default:         ws2812.set(0,0,0);         break;      } } 

however, there possibilities this keywords referenced in other parts in rest of code may suffer issue. may need go check it.


choice#2 > convert prototype functions

this way can retain this keywords, functions no longer static. need instantiate instance object of ws2812 use.

so declarations becomes:

var ws2812 = function(){};   ws2812.prototype.set = function(r,g,b){     $.get( "/light?r="+r+"&g="+g+"&b="+b, function( data ) {         console.log("light set to:"+"/light?r="+r+"&g="+g+"&b="+b);     }) };  ws2812.prototype.speech = function(word){     switch(word){     case "czerwone":         this.set(255,0,0);     break;     case "zielone":         this.set(0,255,0);         break;     case "niebieskie":         this.set(0,0,255);         break;     case "białe":         this.set(255,255,255);         break;     default:         this.set(0,0,0);         break;      } } 

then use via instance of object instead:

var myws2812 = new ws2812(); myws2812.speech('hello world!'); // inside it, should call this.set 

choice#3 > bind 'this' object when calling

in case insist don't want modify implementation of ws2812. it's okay leave , bind object when using instead.

so when call ws2812.speech, need use function.prototype.call , pass in ws2812.set this.

ws2812.call( ws2812.set, 'hello world!' ); 

however, doesn't semantic , may cause confusions in future use people maintain code.


i'll leave decide best way go.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -