I am experimenting a strange issue: at the beginning of my code, I defined a function like so:
function rootEmbed()
{
var embed = new Discord.RichEmbed()
.setColor(config.embedColor);
return embed;
//returns an object
}
Later in the same file, I define another function which calls the one above, like so:
function commandList()
{
var embed = rootEmbed();
//....
}
Calling that function causes no problem, however calling the following function returns an error that says
(node:4988) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: rootEmbed is not a function
Here is the told function:
function voidTrader(trader)
{
var rootEmbed = rootEmbed();
//...
}
I can't figure out why does the voidTrader() method causes an error while commandList() turns out perfectly fine. What am I doing wrong?
If you want the full code, you can find it here.
Thanks in advance!
This is a combination of variable hoisting and variable shadowing. When you initialize a variable var x = 5, what happens is that var x is hoisted to the top of the code file. However, function declarations are hoisted even higher. Meaning that when your file is run, this is what is happening:
function rootEmbed (){//...code here}
var rootEmbed = undefined;
To fix this problem, change this:
function voidTrader(trader)
{
var rootEmbed = rootEmbed();
//...
}
To something like this:
function voidTrader(trader)
{
var rootEmbedResult = rootEmbed();
//...
}
There is another problem at lines 25-26:
var year = "" + stamp.getYear();
var year = twoDigits("20" + year.substr(-2));
year is declared with var twice.
Related
Iam trying to run an external function inside nightmarejs evalute function...As you can see my code below...
function get_my_links(url){
vo(function* () {
var nightmare = Nightmare();
var href_link = []; // i have tried making it as global without var but did not work
var title = yield nightmare
.goto('https://examply/'+url)
.evaluate(function (href_link,url,get_my_links) {
$('.myclass').each(function() {
href_link.push($(this).attr("href"));
});
if($.isNumeric($("#someid").val()))
{
get_my_links(1)
}
else{
return href_link;
}
},href_link,url);
console.log(title);
yield nightmare.end();
})(function (err, result) {
if (err) return console.log(err);
});
}
get_my_links(0)
By above code I am trying to update href_link ...
1) How to make it Global object,so that everytime the function is called new value should be added with the existing values?
1st The reason
// i have tried making it as global without var but did not work
is not working because though you making the object global but every time you call get_my_links function, it will update the global object to empty array.
For your use case, define href_link before defining get_my_links function. Like
var href_link =[];
function get_my_links() {
...
}
Defining href_link after function definition like ->
function get_my_links() {
...
}
var href_link =[];
will throw an error of undefined value of href_link inside get_my_links function due to hoisting which must be the case you have mentioned in above comment.
electron uses node.js, so you can use the global object of node.js to store the value.
https://nodejs.org/api/globals.html#globals_global
When you use this solution you should be able to access the value also from other parts of your app.
Edit: Problem solved, I forgot to put the output for the riteg object before.
Okay, so I'm quite sure how to describe this issue (which is why the title is bad), but this for loop:
function(){
var out = 0;
for(var i = 0;i<gameLists.listAllGen.length;i++){
out += window[gameLists.listAllGen[i]].output();
}
return out/10000;
},
doesn't work at all. In the console, it says: TypeError: window[gameLists.listAllGen[i]].output is not a function
When I replace the [i] with, for example, [0], it works:
function(){
var out = 0;
for(var i = 0;i<gameLists.listAllGen.length;i++){
out += window[gameLists.listAllGen[0]].output();
}
return out/10000;
},
returns out as expected and no errors.
The variables if needed:
var gameLists = {
listAllGen:['solarGen','riteg']
}
and
var solarGen = {
name:"Solar Generator",
count:0,
genRate:0.25,
price:410,
output:
function(){
return this.count*this.genRate*10000;
},
gameLists.listAllGen[0] returns -> solarGen
and you have solarGen in your script, thus window[gameLists.listAllGen[0]].output() referes to the output key and () calls the function assosiated with the key solarGen
But gameLists.listAllGen[1] return -> riteg
and no output property is assosiated with the riteg, thus it, and window[gameLists.listAllGen[1]] it expects. that there is riteg with output as a key aand a function assosiated with it, but it is not able to find that.
Thus you are getting the error
TypeError: window[gameLists.listAllGen[i]].output is not a function
because there is no function by that name.
I have a piece of code shown below that is supposed to select the first object in a list of image object. When it runs, about half of the time I am getting an error:
TypeError: Cannot read property of mylist
I realize that this is happening because it is trying to reference an object that has not yet been given a reference to an object. Basically, the page will load, and I want to call this function in JavaScript, however if this code runs before the list is referenced, it errors out. Here is my code snippet:
window.onload = function () {
try {
var tnl = $find('mylist');
tnl.selectFirstItem();
}
catch (exception) {
alert(exception);
}
}
The
tnl.selectFirstItem()
line is the one throwing the error to the catch block.
So, my question is, is there a way to force it to wait until this object "tnl" has been referenced?
Thanks!
var tnl = $find('mylist');
should be
var tnl = $('#mylist');
assuming mylist is an ID.
You're using find wrong.
First you get your element, then you use it to find a child element.
So you could do this:
var container = $('.containerElement');
container.find('#mylist');
Here is the "loop" you are asking for. But this is still wrong. If tnl is not assigned after $find('mylist'); returns, it is not going to become assigned later. You likely have error thrown by $find call.
window.onload = function () {
try {
var tnl = $find('mylist');
var timer = setInterval(function() {
if (tnl) {
clearInterval(timer);
tnl.selectFirstItem();
}
}, 1000);
} catch (exception) {
alert(exception);
}
}
I make a simple quiz game. Here is some relevan methods that i have inside one object.
But doesn't work. I always get an error within 'rightAnswerGot' function. Console drops
"uncaught typeerror undefined is not a function for object methods" for this.addVariantsHtml(this.updateCharacter());
BasicGame.Game.prototype = {
actionOnClick: function (button) {
var log;
if(button.value==this.char_bubble.text) {
setTimeout(this.rightAnswerGot,1000);
} else {
// wrong
swoshsound.play();
}
console.log(log);
},
rightAnswerGot: function (){
this.addVariantsHtml(this.updateCharacter());
},
addVariantsHtml: function(id) {
this.answer = this.getAnswersVariants(id);
for (var i = 0; i < 6; i++) {
this.button[i].value = this.answer[i]['trans'];
this.button[i].char_id = this.answer[i]['id'];
this.ans_text[i].setText(this.answer[i]['trans']);
}
},
updateCharacter: function() {
var i = this.getRandomCharacter();
console.log("updateCharacter: "+i + " " +this.chars[i]);
this.char_bubble.setText(this.chars[i].getPath());
return i;
}
}
The aim is to froze the game for a second, when user choose the right answer, and than go to next question. Any ideas why does it happens?
Thanks
Looks like a classic JavaScript scope issue to me. However as you've tagged this question as using Phaser, I would suggest you use a Phaser Timer event to avoid scope problems. Specifically:
setTimeout(this.rightAnswerGot,1000);
replace it with:
this.game.time.events.add(Phaser.Timer.SECOND, this.rightAnswerGot, this);
Which will create a single 1 second timer that fires only once, calling your function at the end of it. You can use 1000 instead of Phaser.Timer.SECOND of course.
I would image that whats happening is that its trying to call the this.addVariantsHtml method, before its calling this.updateCharacter and getting the ID.
So your probably expecting that when it runs, for it to be something like:
this.addVariantsHtml(1);
But its actually trying to run
this.addVariantsHtml(this.updateCharacter());
So just do this:
var id = this.updateCharacter();
this.addVariantsHtml(id);
Either that or you need to look into method chaining/piping, which is just complicated and doesnt need to be used for this situation, but is interesting :)
Ok I found something that made it work!!
Here is a solution:
actionOnClick: function (button) {
var log;
if(button.value==this.char_bubble.text) {
var context=this;
setTimeout(function() {
context.addVariantsHtml(context.updateCharacter());
},1000);
} else {
// wrong
swoshsound.play();
}
console.log(log);
},
This is some JS code
var methodArr = ['firstFunc','secondFunc','thirdFunc'];
for(var i in methodArr)
{
window[methodName] = function()
{
console.log(methodName);
}
}
My problem is that how to get the name of a function in JS.
In JS, use this.callee.name.toString() can get the function name. But in this situation, it is a null value. How can i get the 'funName' string?
Sorry, I didn't make it clear.
I want to create functions in a for loop, all these functions has almost the same implementation which need its name. But others can call these functions use different name.I want to know what methodName function is called.
it seems a scope problem.
Try this:
var methodArr = ['firstFunc','secondFunc','thirdFunc'];
for(var i in methodArr) {
var methodName = methodArr[i]; // <---- this line missed in your code?
window[methodName] = (function(methodName) {
return function() {
console.log(methodName);
}
})(methodName);
}
window['secondFunc'](); // output: secondFunc