Yesterday, a friend asked such a question.CodeI have done a lot of tests in the past, but I found that when I tried to find out the cause from the root cause, I could not help but record it. I will verify it later.
Simple question: VaR con = function (){}; VaR A = con. prototype = []; var OBJ = new con; obj. push (1); alert (OBJ [0]); alert (obj. length); // IE 6 7 8 (in 8 weird mode) outputs 0, IE8 standard mode, and ie9 and other browsers output 1 where Ie 1 is pushed. obviously, the browser has this [this. length ++] = logic similar to value. (This is certainly not the case for code maintenance. length may vary with browsers, which is also one of the different implementations of IE 6 7 8.) Another version: interface obj. push (1); we re-obj. push (2); alert (OBJ [0]); alert (OBJ [1]); tragedy occurred. the reason is. IE6 7 8 (weird mode) is not properly maintained to this. length attribute. let's change our thinking: var OBJ = {length: 0 };
[]. Push. Call (OBJ, 1 );
[]. Push. Call (OBJ, 2 );
Alert ([OBJ [0], OBJ [1], obj. length]); the behavior of all browsers is normal this time. okay. originally, only OBJ itself had. the Length attribute is not inherited from the prototype chain. it seems that IE is OK. we will return to the initial version. and make some modifications. try again. vaR con = function (){};
VaR A = con. Prototype = [];
VaR OBJ = new con;
OBJ. Length = 0;
// Obj. Push (1 );
// Obj. Push (2 );
A. Push. Call (OBJ, 1 );
A. Push. Call (OBJ, 2 );
Alert (OBJ [0]);
Alert (OBJ [1]); IE6 7 8 is still an old problem .. this. length is not properly maintained. specific reasons .. I am still struggling... I have made several attempts. for example, modifying a constructor or even. constructor has not changed much. the only thing I cannot touch is obj. _ PROTO. finally, leave a guess. when OBJ _ PROTO _ points to an array. IE6 7 8 is a brain attack .... next. we may try some other special objects... it also caused some strange problems.
About arguments. First, the performance of this ie series is not good. at least consistent with the behavior of most browsers... none of the browsers I know currently comply with the ecma262 V3 or V5 specifications. before arguments and array prototype work together. we also need to make some small demos to illustrate some problems. most people may know about it. the interesting question recently mentioned in his blog is: function f (){
Arguments [0] = 10;
Alert ();
}
F (); except chrome or a browser that uses its V8 engine, undefined is printed. the reason is that Chrome's implementation of arguments is different from that of other browsers... obviously, Chrome is making a huge profit. always associated. maintain the relationship between arguments [Index] and corresponding parameters... the problem with other browsers is that the actual parameter quantity does not match. they are also associated. however, the associated maintenance object is not an arguments but a member that can be accessed through an index .. (Refer to ref variable)
And Chrome has a very tragic bug...
(Chrome15 has been fixed. whether argument or eval exists. in the following code, a is in undefined. the reason is es5 compliance. no longer unconditionally share the values of the corresponding members of the form parameters and arguments)
Object. Prototype ['0'] = 'franky ';
Function f (){
Alert ();
Arguments; // If you comment out the arguments call, alert (a) results will be completely different.
// Eval ('');
}
F ();
Eval ('') and arguments; both can print Franky instead of undefined .... in fact, this bug is similar to a bug in the earlier ff (earlier than ff3.0) spider monkey engine. object. prototype. ATTR = 'franky '; void function (){ VaR ATTR = 'ooxx '; Void function FN (){ Alert (ATTR); // It is a great honor to print out Franky, not the ooxx we expect. } () ;}(); See? The same problem. Early fF was designed to comply with ECMA standards. (to implement the "name" function expression, you can only find your own function name in the function's own scope .) put an object to the top of the current scope chain object. and this object. _ PROTO _ = object. prototype .. I forgot to take extra insurance measures. this problem occurs.
Let's look back at what Chrome is doing... obviously, Eval and arguments have killed some of its protection mechanisms for the arguments object .... At first, I thought so. but think about it carefully... arguments; or eval ('') is put after alert and takes effect... the problem should be caused by lexical analysis. instead of the execution period, we can infer that when chrome analyzes the context of a function subject, it will see if you have arguments or eval. If there is one, it will be given to this function arguments object, otherwise, no. this is essentially an optimization. that is to say, chromeProgramNo protection mechanism is provided for arguments to prevent them from accessing object. prototype.
The arguments for proving this inference are not very adequate, but I still try to give two... This bug is also triggered if Eval is used. so can we consider it. eval has a unique execution context that is subordinate to the current execution Context Environment (acting on the current scope. he only has arguments. it is the arguments of the current function. therefore, Chrome has to dispatch the arguments object to the current function object when eval appears. this bug occurs .. Because not only Eval, new fcunction (), window. eval ('') also has the characteristics of dynamic statement execution .. the difference lies in the scope. that is, except eval (''), the other two methods cannot access the arguments of the current function.
To encourage chrome, let's talk about an interesting question about other browsers:
Function f (a, B, c ){
For (VAR I = arguments. length; I --; []. Shift. Call (arguments ));
Alert (arguments [0]);
Alert ([a, B, c]);
}
F (1, 2, 5 );
This is a special example. If you have enough browsers, you will look at the screen with a dull expression as I used to. Fortunately, I found the answer. IE 8: In standard mode, IE6, IE7, ie9, FF, Safari, opera9.6: Undefined 5, 5 IE 8: In Weird mode, Opera 10: 5, 5, and 5ie 8 can be completely interpreted as a brainless behavior in the weird mode. this is because IE 8 may be compatible with some code on earlier pages. in this modeWindow. JSON is all killed. In some SHANZHAI browsers, when the kernel is the same as IE8, the so-called compatibility mode is directly enabled, and there is no JSON. Therefore, if you run this test in a browser using the IE8 kernel, the first rate will be 5, 5, or 5, unless you have a way to make him incompatible. What I cannot understand is. Why is the IE8 JScript engine so strange when it enters the so-called compatibility mode? Who is it compatible? Is it ie5.5?
Let's take a look at opera10... This browser has always been speechless. Many features are often changed. It's always good and bad. It's ignored. Now let's explain the cause of 5, 5, and 5... the shift method works like this: arguments [0]: 1 arguments [1]: 2 arguments [2]: 5
The first shift is to modify the value of [0], that is, to [0] the value of [1]. then, the value of [2] is given to [1]. then delete arguments [2]. as you can imagine, if the delete statement is valid three times later, the values of [0] [1] [2] should be undefined because these three keys are removed from the arguments object. but they are associated one by one (with the same stack reference or memory address. the specific situation depends on the data type.) The parameters A, B, and C keep the new value assigned before the delete operation... naturally, they are all 5... opera10, arguments [0] is not deleted successfully. I can understand it. the opera Team intends to comply with ecma262 V5 specifications... strictly restrict arguments operations. for ie8 .... I still cannot understand .... because ie9 does not pay tribute to the standard either .. what's more, the so-called compatibility mode of IE8? I also want to continue writing other methods on the array prototype, such as the differences between different splice sclie browsers and browsers such as IE nodelist and ff. length read-only features .... and push shift unshift pop, etc. Why is it more obedient in IE... suddenly I found myself physically inactive... if you are interested .. try it by yourself. PS: the reason why the title is object rather than arguments is also true... but now it seems that there are some titles.