admin管理员组文章数量:1416082
I'm trying to understand why I get "variable may not have been initialized" when I test (variable === "some text") but I dont get when I used (typeof passwordHashOrg !== 'undefined')
The working code:
checkPass('share', function (error, response) {
"use strict";
if (error) {
console.log(error);
} else {
console.log(response);
}
});
function checkPass(username, callback) {
"use strict";
// Require
var fs = require('fs');
// Static Parameters
var usernameInput = username;
// Open shadow file
fs.readFile('/etc/shadow', function (error, file) {
if (error) {
return callback(error); // file does not exit
}
// file is a buffer, convert to string and then to array
var shadowArray = file.toString().split('\n');
var passwordHashOrg;
shadowArray.forEach(function (line) {
var shadowLineArray = line.split(":");
var usernameOrg = shadowLineArray[0];
if (usernameOrg === usernameInput) {
passwordHashOrg = shadowLineArray[1];
}
});
if (typeof passwordHashOrg !== 'undefined') {
callback(null, 'userExist');
} else {
callback(null, 'unknownUser');
}
});
}
and the code that I get "variable may not have been initialized" :
checkPass('share', function (error, response) {
"use strict";
if (error) {
console.log(error);
} else {
console.log(response);
}
});
function checkPass(username, callback) {
"use strict";
// Require
var fs = require('fs');
// Static Parameters
var usernameInput = username;
// Open shadow file
fs.readFile('/etc/shadow', function (error, file) {
if (error) {
return callback(error); // file does not exit
}
// file is a buffer, convert to string and then to array
var shadowArray = file.toString().split('\n');
var passwordHashOrg;
shadowArray.forEach(function (line) {
var shadowLineArray = line.split(":");
var usernameOrg = shadowLineArray[0];
if (usernameOrg === usernameInput) {
passwordHashOrg = shadowLineArray[1];
}
});
if (passwordHashOrg === 'some text') {
callback(null, 'userExist');
} else {
callback(null, 'unknownUser');
}
});
}
The only diferent from the two code is:
if (typeof passwordHashOrg !== 'undefined') {
vs
if (passwordHashOrg === "some text") {
I'm trying to understand why I get "variable may not have been initialized" when I test (variable === "some text") but I dont get when I used (typeof passwordHashOrg !== 'undefined')
The working code:
checkPass('share', function (error, response) {
"use strict";
if (error) {
console.log(error);
} else {
console.log(response);
}
});
function checkPass(username, callback) {
"use strict";
// Require
var fs = require('fs');
// Static Parameters
var usernameInput = username;
// Open shadow file
fs.readFile('/etc/shadow', function (error, file) {
if (error) {
return callback(error); // file does not exit
}
// file is a buffer, convert to string and then to array
var shadowArray = file.toString().split('\n');
var passwordHashOrg;
shadowArray.forEach(function (line) {
var shadowLineArray = line.split(":");
var usernameOrg = shadowLineArray[0];
if (usernameOrg === usernameInput) {
passwordHashOrg = shadowLineArray[1];
}
});
if (typeof passwordHashOrg !== 'undefined') {
callback(null, 'userExist');
} else {
callback(null, 'unknownUser');
}
});
}
and the code that I get "variable may not have been initialized" :
checkPass('share', function (error, response) {
"use strict";
if (error) {
console.log(error);
} else {
console.log(response);
}
});
function checkPass(username, callback) {
"use strict";
// Require
var fs = require('fs');
// Static Parameters
var usernameInput = username;
// Open shadow file
fs.readFile('/etc/shadow', function (error, file) {
if (error) {
return callback(error); // file does not exit
}
// file is a buffer, convert to string and then to array
var shadowArray = file.toString().split('\n');
var passwordHashOrg;
shadowArray.forEach(function (line) {
var shadowLineArray = line.split(":");
var usernameOrg = shadowLineArray[0];
if (usernameOrg === usernameInput) {
passwordHashOrg = shadowLineArray[1];
}
});
if (passwordHashOrg === 'some text') {
callback(null, 'userExist');
} else {
callback(null, 'unknownUser');
}
});
}
The only diferent from the two code is:
if (typeof passwordHashOrg !== 'undefined') {
vs
if (passwordHashOrg === "some text") {
- 1 Seems to me that you don't have to initialize a variable in order to pare its type. Perhaps an uninitialized variable's type is already 'undefined'? – mah Commented Jun 30, 2015 at 19:34
- Is this a jslint or jshint warning? Please tag your question appropriately. – Bergi Commented Jun 30, 2015 at 19:40
- The problem is: I'm trying to look for a specific user in a linux shadow file and if that user exist do something. The question is: why do I get 'unknownUser' when I try to test if variable 'passwordHashOrg' have some value but if I test for defined it success. – Adrian Gherasim Commented Jun 30, 2015 at 19:47
-
@AdrianGherasim: Maybe because the shadow file is unlikely to contain "
some text
"? – Bergi Commented Jun 30, 2015 at 19:53
1 Answer
Reset to default 4The warning means just what it says - the variable might not have been initialised with a value at the time of the parison. Since that's the expected behaviour, and basically just what you want to test for, the warning can be safely ignored. Alternatively, use
var passwordHashOrg = null; // or
var passwordHashOrg = undefined; // or whatever default value you want
So why didn't you get the warning when using typeof
? Because that's not necessarily evaluating the value of the variable, and might be seen by jshlint as justified on non-initialised variables (it even works on undeclared variables). You'd probably get the same warning if you did pare the value to undefined
.
本文标签: javascriptvariable may not have been initialized when testStack Overflow
版权声明:本文标题:javascript - variable may not have been initialized when test - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745244480a2649493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论