各位版上的大神们好,想请问各位:
let age = 27;
age.toString();
if (age===27) {
console.log("number");
} else if (age==="27") {
console.log("string");
} else {
console.log("I don't know.");
}
这个例子中,结果会是‘number’,因为 age 是 number,只有 age.toString 是 string 对吗?
第二个例子:
let friends = ["John", "Sandy", "Alex", "Jim", "Greg"];
let friends = ["John", "Sandy", "Alex", "Jim", "Greg"];
friends.push("Harry");
console.log(friends);
这个例子中,结果会是["John", "Sandy", "Alex", "Jim", "Greg", "Harry"]
但,为什么第一个例子中 age 使用了 .toString() 后,‘age’ 本身并没有变成 string;但在第二个例子中,friends 使用了 .push("Harry") 后,‘friends’本身却改变了?
感谢各位!
作者:
laechan (挥泪斩马云)
2022-06-20 17:01:00你只能习惯有些就是这样,有些就是那样(如 array.sort() )不想记就是写code时多花一些时间在debug我是更懒连push是啥平常都没记,要用时google就好了像VB是Cint/Cstr,到javascript变 parseInt/toString = =若你常写常用则自然会知道啥时要用 变量=xxx, 啥时不用