What methods toString and valueOf are called when a reference type is implicitly converted to a comparison operator? (last Year’s Spring Festival was very noisy.)
if(a == 1 && a == 2 && a == 3){
console.log("I walked in."); } <! Var a = {num:0}; a.valueOf =function() {return++a.num } <! -- answer 2: --> var num = 1;function a() {return num++;
}
if(a() == 1 && a() == 2 && a() == 3){
console.log("I walked in."); } <! Var num = 0; Function.prototype.toString =function() {return ++num;
}
function a() {} <! - the answer 4: - > var a = {[Symbol. ToPrimitive] : ((I) = > () = > + + I) (0)};Copy the code
2. What does this point to? (The last output question is a reference to the netease interview question)
var names = "Mr. Song Wei";
var obj = {
names:"Mr. Zhang Jian",
showName:function(){
console.log(this.names);
},
returnName:function() {return this.names;
},
returnFunctionName:function() {return function(){ console.log(this.names); } } } obj.showName(); // Output what?"Mr. Zhang Jian"obj.returnName(); // Output what?"Mr. Zhang Jian"obj.returnFunctionName()(); // Output what?"Mr. Song Wei"obj.showName.call(names); // Output what? undefined obj.returnName.call(names); // Output what? ReturnFunctionName ().call(names) // Output what? undefined var newObj = obj.returnFunctionName().bind(window); Newobj.call (obj) // Output what?"Mr. Song Wei"// Why the last output"Mr. Song Wei"? becausebindIf you call this object again after pointing to this object, this point will not be changedCopy the code
3: Again this points to the question? (If you can’t look at the answer, you can tell me the result.)
var big = "Teacher Wanda";
var obj = {
big:"Mr. Song Wei",
showBig:function() {returnthis.big; } } obj.showBig.call(big); / / ƒbig() {[native code]Copy the code
4: What is the length of this? (Again a “this” point to estimate can understand this classmate this point should not be able to stump)
functiona(a,b,c){ console.log(this.length); //4 console.log(this.callee.length); / / 1}function{fn (d) the arguments [0] (10,20,30,40,50); } fn (a, 10, 30); // The first output: because this is currently pointing to arguments. Arguments is a pseudo-array with the length attribute. Arguments are used to hold the arguments to the function. Fn is called with four arguments. So arguments has length 4. This time arguments[0] is equivalent to arguments.a calling this function. Callee is a property of Arguments that returns the body of a function directly under the current arguments. So this.callees is returning fn. Each function has a length attribute which is primarily used to return the parameter of the function so it's 1.Copy the code
5: The classic variable promotion problem?
= = = = = = = = = = = = = = = = = = = the topic a = = = = = = = = = = = = = = = = = = = = = = = = = = =if(!"abc" inwindow){ var abc = 10; } console.log(abc); // The default value of ABC is undefined because the variable is declared to be promoted first. ! ABC forfalse ,inIs to check whether an attribute exists in an object. It is clear that thefalseBelonging is a Boolean type. Does not exist in the object. So I didn't goifVariable assignment. // For the question raised by Wens. Let me restate that ABC claims to have been promoted. butifIt's judging the string ABC. So!"abc" 是false. You can tryif(!"abc"){var abc=10} console.log(abc); Try againif(! abc){var abc=10} console.log(abc); See if the results as the = = = = = = = = = = = = = = = = = = = question 2 = = = = = = = = = = = = = = = = = = = = = = = = = = = the console. The log (a); //undefinedif(! ("a" inwindow)){ var a = 10; } console.log(a); // The value of a is assigned to undefined by default because the variable is promoted first. The variable promotion will be in GO which is window. So,"a" inWindows) must betrue. ! If I go the other way, I'm going to go the other wayfalse. So no assignment. = = = = = = = = = = = = = = = = = = = question 3 = = = = = = = = = = = = = = = = = = = = = = = = = = = var x = 1;if (function f(){}) { x += typeof f; } console.log(x); //1undefined // because the function will run as an expression in (). Finally convert totrueThere will be no overall function declaration promotion. So typeof is undefinedCopy the code
6: Interview chance 80% of the questions!
function fun(n,o) {
console.log(o)
return {
fun:function(m){
returnfun(m,n); }}; } var a = fun(0); a.fun(1); a.fun(2); a.fun(3); Var b = fun(0).fun(1).fun(2).fun(3); Var c = fun(0).fun(1); c.fun(2); c.fun(3); Undefined 0 1 1 // The answer is obvious. Let me do it in a different formfunction fun(n,o) {
console.log(o)
return {
fun:function(m){
returnfun(m,n); }}; } var a = fun(0); a.fun(1); a.fun(2); a.fun(3); // null null null null null null null null null null null null nullfunction fun(n,o) {
var n=0;
var o;
console.log(o) //undefined
return {
fun:function(m){
returnfun(m,n); --> n gets fun where n is 0. Then call fun once and the following function display appears. }}; } // a.run (1) = fun(1,0)function fun(n,o) {
var n=1;
var o=0;
console.log(o) //1
return {
fun:function(m){
returnfun(m,n); --> n gets fun where n is 0. }}; } // And so on. I don't know if my friends understand it, because I feel that the interpretation of language is not as good as the differentiation codeCopy the code