Javascript variable is undefined but it is defined - javascript

I've this method in my global controller object of my JavaScript application. Now I get the error, that the statement self.texts.buttons.disabledFinishedJobs is undefined. But I don't understand that because the console.log() statement outputs the expected value. What can be the reason?
toggleFinishedJobs: function() {
var self = this;
console.log(self.texts.buttons.disabledFinishedJobs[0]);
if (this.disabledFinished) {
$(".status_99").show();
this.disabledFinished = false;
$("btn_finishedJobs").text(self.texts.buttons.disabledFinishedJobs[0]);
} else {
$(".status_99").hide();
this.disabledFinished = true;
$("btn_finishedJobs").text(self.texts.buttons.disabledfinishedJobs[0]);
}
}

Try this:
$("btn_finishedJobs").text(self.texts.buttons.disabledFinishedJobs[0]);
^-Typo error
instead of
$("btn_finishedJobs").text(self.texts.buttons.disabledfinishedJobs[0]);

Looks like you have a typo at the end of your code. self.texts.buttons.disabledfinishedJobs instead of self.texts.buttons.disabledFinishedJobs.

Related

Undefined results on calling module.exports in nodejs

my app.js contents:
var greet3=require('./greet3');
greet3.greet();
my greet3.js contents
function Greetr(){
this.greeting='greet3';
this.greet=function(){
console.log(this.greeting);
}
}
module.exports=new Greetr();
my console window output : greet3
but when i change the contents of app.js as :
var greet3=require('./greet3');
greet3();
i get the ouput as 'undefined'
I am a beginner in node.js and was trying different module patterns in it.I need help on why I am getting the result as undefined
Given your above module, the line
var greet3=require('./greet3');
essentially becomes:
var greet3 = {
greeting: "greet3",
greet: function(){
console.log(this.greeting);
}
}
So in app.js, greet3 is now an Object. greet3.greet(); works as expected and logs "greet3".
But greet3() produces greet3 is not a function, since greet3 isn't a function.
You are exporting a initialised object, when you set var greet3=require('./greet3') and call greet3() you are trying to use the object as a function, hence it says undefined.
Change your code in greet3.js:
module.exports = function(){
this.greeting='greet3';
this.greet=function(){
console.log(this.greeting);
}
}
As others said before, your code is working as expected.
Your module:
function Greetr(){
this.greeting='greet3';
this.greet=function(){
console.log(this.greeting);
}
}
module.exports=new Greetr();
Returns an object, but I think you wanted to return a function, so the last line should look like this:
module.exports=Greetr;
Instead of this:
module.exports=new Greetr();
Because now when you call:
var greet3=require('./greet3');
greet3();
It assigns object of function Greetr to a variable greet3 and you cannot call an object.

SCRIPT1003 error in IE

In my project, I make a require call to a js file to create an object of the file.
For example,
require(['directory/hello-js-file'], function(Hello){
var hello = new Hello();
hello.show();
});
After this block, Hello is undefined. In the IE console, I see this error message, 'SCRIPT1003: Expected ':' File: Function code (13), Line:2, Column:1'
The function looks like this:
function anonymous(){
return eval(arguments[0]);
}
I was able to narrow down this issue being originated from two functions (Contents have been modified but the logic is same.):
bindDetail: function($view){
var self = this;
var details = object.details; // object is a global variable
for(var i = 0; i < details.length; i++){
if(self.hasData(details[i])){
// doSomething
}
}
},
hasData(detail){
if(detail.data !== undefined && detail.data !== ""){
return true;
}
return false;
}
This issue only arises in IE. This works fine in Chrome.
Can someone please direct me to a solution? Thank you. I appreciate your help.
After posting the question, I realized that I was missing the :function tag to hasData. That resolved the issue. :)

Javascript function undefined return

<script>
var tarih = tarih();
alert(tarih);
function tarih() {
var s=emrah;
return(s);
}
</script>
Bu fonksion neden undefined dönüyor ?
Why "undefined" returns?
The reason it returns undefined, is because of the assigment
var s = emrah
is confusing JS. So, it looks for a var name emrah and doesn't find it. That is the reason, you get undefined. Plus, if you are looking at your console, it would already have given you an error message, that
emrah is not defined
You might want to try: (If that's what you wanted)
var s = 'emrah'
You are calling the method before its defined.
Change the code to something like this.
<script>
function tarih() {
var s='emrah';
return(s);
}
var tarih = tarih();
alert(tarih);
</script>

Something is wrong in JavaScript object declaration

This code:
var doc = {
foldPrompt: function(folded) {
return folded ? "Click to unfold" : "Click to fold"
},
createFoldButtons: function() {
var prompt = foldPrompt(true); //The error is here
$("#ComparisonTable td.secrow").each(function(index, td){
$(td).prepend($('<img src="minus.gif" class="foldbtn" alt="'+prompt+'" title="'+prompt+'">'));
});
}
}
gives me an error: Undefined variable: foldPrompt
What am I doing wrong?
foldPrompt is not a variable; it's a property of doc, and you need an object reference to access properties of that object.
If someone calls doc.createFoldButtons(), then the this context variable will point at the same object that the doc variable does. So, replace foldPrompt(true) with this.foldPrompt(true).

Why does this function return as undefined?

String.prototype.parse = function(f) {
alert(this.replace(f, ""));
};
var a = "Hello World";
parse.apply(a, ["Hello"]);
Is the code correct?
No, that’s not correct. The function is defined as String.prototype.parse, so it is not available as parse (in fact, parse is undefined).
You could run it like the following:
String.prototype.parse.apply(a, ["Hello"]);
But actually, the reason why you add the function to the prototype of String is that you extend String objects with that function. So you actually should just run the function like this:
a.parse("Hello");
edit:
Oh, and in response to your question title “Why does this function return as undefined?”: The function doesn’t return anything, because you don’t tell the function to return anything. You could for example define it like this to return the replaced string (instead of alerting it):
String.prototype.parse = function(f) {
return this.replace(f, "");
};
And then you could alert the return value of the function:
alert(a.parse("Hello"));
There is no such variable parse defined in your code sample. If you really want to apply the function later on, you should do this:
// Capture function as a local variable first
var parse = function(f) { alert(this.replace(f, "")); };
String.prototype.parse = parse;

Categories