First, when we discuss how to add the ToString method to all the method window objects, let's first say that the object inheritance and object instance of window , and the this pointer of the constructor, There are also the methods of raising the variables and the method, and finally, the way to deal with the window object and the window method (function).
Before you say the window object, please let us write an example of the method of warm, diving should be done before the warm-up action, although we do not dive, but write code also need to do a warm-up, so as to adapt to the following high-difficulty action.
A lot of crap, the referee can not look down, and begin to prepare your posture, let us start to take a full mark, frighten the referee, but see the referee frightened himself is not good! I said to amuse myself, we should prepare for the next, go ...
var fun = new Object(); see this you will not be unfamiliar, this is an example, compared to var win = new window(); In fact, this will be wrong, it is obvious that the global property itself is an instance object. So we're going to write an object that is relative to an instance of object to output object {},var win = new window. Object (); So it's not going to go wrong. This win is an example, then the instance can also be simplified, such as var win = {}.
Now, do you think, so I want to put the global window also write their own, Wah, this advice is good, you can try, so we immediately write a can replace the browser's global properties, var window = {}; Output a global empty object, no problem, But it is not the browser-specific properties, this is of course, we'll say later, here to leave a suspense.
The referee said, you this diving, jumping or general-like, to the handsome feeling is not before the standard, so, the manager patted my shoulder, lad, come on! I know this is to cheer me up, cheer me up, I shouted, I go! Can't you see that I'm jumping so hard? Look at the sky a line of words: Friendship Boat said turn over! See the audience downstairs are looking at their own, so I pretended to talk about, pop a sound, jumped down, well, you TM is kidding me, so it's over.
Of course, otherwise I write this code to God horse, fast one o'clock in the morning, who will be here to see you, Speed Point bar.
Now we're at the instance, var fun = new function (); It's familiar to you, and if you think you've seen it, then you know that function is a complete constructor. So the simplified function should be how to write it, function fun () {}; Here is a shorthand, extensible constructor, with this condition, you must be thinking, since you can expand, then I can not add material to the function, so that the function becomes more flexible, so, there are the following different functions of writing methods, such as: var Koringz = function () {}; see this, you will feel, is not still able to add material to the function, var koringz = function Koringz () {}; it looks a little dazzling, you have so much halo, I TM How to know you, All kinds of pack B skills are concentrated together, can you do a bit more? I just want to see how many times these letters can be combined, say combination, (function () {}) (), var Koringz = (function () {}), two,! function () {} () three kinds, +function () {}) () four. My eyes are not good, you still slow down, we still slow down the combination, said the words are gone, maybe I think this is not too fast. If you think there are more, you can continue to add material, Moe!
With the above various forcing lattice, you still feel that these are not enough for you to install force. So below, I think the move play hardball guy, skill for on, with skill may be more than useless skill of the force lattice notch above, that also wait for what, say dry bai.
We define a window global variable and give the Window object a variety of custom properties.
var windows = {
Top: ' Window ',
Local: ' Local ',
Window:function() {},
Attr:function Attr () {},
Koring:koringz,
ZSG:ZSG,
Tr:trs,
Wyz:weiyuzhou,
Call:cal
};
These properties you are familiar with it, look at it, see if you have the right to see, have an article have to remember the name.
Below I'll give it a way to add properties:
var Koringz = function Koringz () {koringz.tostr = function () { }; Koringz.build = [' Tostr ', ' defined ToString ']; This. CHECKING = ' 1 ' ;} var zsg= function () {}var Weiyuzhou = function weiyuzhou01 () {}var tostr = function () {}
Koringz. prototype. util = function () {}
Koringz. prototype. UTIL2 = function () {}
Koringz. prototype. util3 = function () {}
Koringz. prototype. Util4 = {};
Koringz. prototype. UTIL5 = {};
Koringz. prototype. UTIL5 = [' 2014-2015-2016 ']
Koringz. prototype. util6 = ' 2014-2015-2016 '
With the above-mentioned custom function expressions, can we go to the global acquisition, if I want to see the effect immediately, whether it is a bit urgent now, remember what we originally promised, I want to give window binding inheritance and this pointer, even add an instance. Right, let's give this to the global object rack on a variety of tripod, so that the Window object has more skills to release, be careful not to be the skills of the Thunder down.
So, let's start with an example: var po = new window. koring (); and then what to do, directly to meet the skills, I think it is still a bit of energy, wait a big move, one after another will be very tired, as well as put together to deal with, good good idea, So it's settled.
Well, we'll define a variable for the PO to pass var pon = PO; Then we give the function sleeve to inherit pon.__proto__ = Po.prototype;. Then we look at the call of the function, whether the this pointer has changed, and whether the inheritance has changed.
Through Console.log (window); Console.log (pon.util6= po.util6); You have found that the variables defined below thewindow property are processed in advance!
NULL Build: array[2null0"korings"korings Tostr: ()tostring: () __proto__: ()
And the output of the Util6 Boolean value is true, wow, good, not too chicken, the original we defined within the Koring function the properties of this pointer are not added in this. CHECKING = ' 1 ', but added through Koringz defined properties Build:array[2] Tostr: (), indicating that the pointer changed at this point, is implemented by Koringz, then where is this point? In this problem, we find that this point is pointing to other places where the this pointer always points to the function name and does not point to the variable name. The output this sees the result as follows:
korings " 1 " "1"korings
The this pointer is able to get the method name on the prototype through the variable.
. __proto__: Korings.constructor:korings (). Util: (). UTIL2: (). UTIL3: (). UTIL4: Object . util5:array[1].util6: " 2014-2015-2016 " . __proto__: Object
However, I bind koringz.checking below a this pointer to see if I can indirectly get the checking method below prototype. The property that is found to bind to the variable cannot be obtained, so we know that the object property of the var Koringz variable cannot be called directly, but the object property of the var Koringz variable can be saved. The object properties on the Korings method can be called directly and cannot be saved directly, so we need to save the object properties on the Korings method in Var The object property of the Koringz variable is bound to a this. property is assigned to the object property on the left variable, so you can indirectly save the parameter value of a Korings method through the object of the var koringz variable. So, you can open your brain and use this to apply a lot of dynamic data, congratulations on getting a chance to rise.
Next, let's see if we can call the properties on the prototype, that is, the object property of the variable or the method property of the variable, and see that the relationship is a bit complicated, and I think this is a function name, so can I bind the pointer of the variable to this pointer, so I'm going to bind this variable to this one, So Koringz = This we can indirectly convert directly from the object property of the variable to get a value or a method, and at this time, we can not only get the object properties on the variable, but also to save the variables of the object properties of the output value and method, and also can be called externally, really is a lot more. Little friends, I'm a little sleepy, I can only write so much.
But let's finish, I'm still at the speed point.
Now I'm wondering how to add a ToString property to all the sub-objects under the window object, and we find that the browser's window object is called by a method that has a string. So we can also implement this tostring method, then we use in to add a method for each window object, in fact, we can also add the ToString method for each function expression, but this is not ideal, So we use the following method, concise and clear:
Stostr Stostr (){}
for (var in windows) { if(typeof windows[p] = = ' function ' Stostr
}
}
See the output value, each method below has a ToString method :
. Attr: Attr(). Arguments:null. Caller:null. Length:0. Name:"Attr". Prototype:attr. toString: Stostr (). __proto__: ().<functionScope>. __proto__: ().<functionScope>. Koring: korings(). Arguments:null. build:array[2]. caller:null. Length:0. Name:"korings". Prototype:korings. Tostr: (). toString: Stostr (). __proto__: ().<functionScope>. Local:"Local". Top:"window"
At this point, each method has a ToString method. Do you feel so easy!
So we're going to call the ToString method Again, how should we call it, and for that, I think there's a basis for that, and here we're going to add a call to all of the Window object's methods. ToString method, so we call directly, this is definitely not possible, he must be built on the this Pointer, so we still return to prototype inherited method, we make a revision on the original code to see if it can be set up in for in inside the statement, we implement one inheritance:
function () { return ' This is Trust tostring method '}
We then output two instances of the object to see how the effect:
var New windows.tr (); var New windows. Attr ();
+ ' = = ' + cas_attr.tostring ());
Output results:
This This is trust ToString method
This is set up! This is the end of the issue.
If you want to expand the toString resource, you can study it on your own, I will not say this (Objectprototype. ToString). For me this lazy person, is generally used in a well-expanded method, directly to generate a string:
var s = (Math.random ()). ToString (+); s.substring (2,22); return " Kcb0f95b4d2832j87d17 "
Get a random string. For this I would like to understand more thoroughly, and said otherwise, I like to use their own style to deal with, have time to integrate, expand the toString method.
In the end, if we say the difference between the window object and the window function, you may have seen that the object is a function of course different, right, as you can see, the properties below thewindow are not The ToString method, so we do not have to give the Window function below to add a string deep to call the ToString method, but you can also expand it yourself. Next I think I should sleep. If you feel that it is useful to you, please pay attention to, thank you.
How would you add the ToString () method to a global object