Speaking of Funciton, is also my to JS very spit groove of a little, encapsulation let my eyes blind, horse eggs, brother can only big eyes open to the use of the black box, almost completely listen to all kinds of books on
function of the way to listen to the pictures, completely did not do a little bit of seeing as reality.
One: what is function
In a long long time ago, we only know that functions is a function, in C # words is a method, since it is the method, it must be hanging under the class, but in fact?
In JS, the function is the object, not the method of our inertial thinking. What is the evidence? Look at the code.
You can see that I've declared a say function, but I'm surprised to see why the Say function has some properties and methods. such as the tostring,valueof that belong to object,
Constructor, wait? And then more curious to see what the constructor is, you can see is actually a function constructor, then I am very clear, then say just
is an instance reference to a function object, knowing this I can easily restore the code as follows:
Two: Look at some properties and methods in function
Now that we know that function is actually an object, and we know that all reference types are inherited from object, I define an instance of object for the sake of comparison.
Let's look at the specific properties and methods in function.
As you can see, sub-class function is really a bit of something of their own, then the following is a simple discussion of common properties and methods.
1. Arguments Properties
When it comes to the nature of the special, the Eye Blind, the blind place lies in the arguments attribute inside actually do a lot of things, so that some of the following strange phenomenon
May make you dumbfounded!!!
<1> Strange phenomenon One: I have no formal parameter in my function, actually can also accept the value of the argument ...
<2> Strange Phenomenon 2: Formal parameters and arguments are not uniform, can be more transitive, can be passed less, JS will not error.
。
<3> Strange Phenomenon 3: Formal parameters and arguments incredibly can do synchronization, too magical.
Does the above three phenomenon make you feel very strange??? These strange phenomena make you feel that the function in the package is too harsh, because they do a lot of operations, and you can only big eyes
Stare small eye, can not see anything ... Isn't it very regrettable??? Because I can't see the source code, I can only understand it according to the explanation in the book and my own understanding. In "Elevation 3"
So, when the arguments passed in the call function are actually given an "internal array" in the function constructor, and arguments is actually a high-level package of "internal arrays,"
The encapsulated arguments is no longer an array, but a disguised array, because arguments also needs a unique property of its own callee, and this callee
Save is the current object say, so can only make arguments object, I can let you see the truth.
And then the weirdest problem is that formal parameters can be synchronized with arguments, and since it's possible to synchronize, my first response is to use the same memory address, but think about it.
How could they have shared memory addresses? But if you think about it, arguments is the encapsulation of the "internal array," and I think the name should have been manipulated, which means
Name is also an "internal array" encapsulation, just like ECMA5 in the field to provide Get/set accessors, of course, this is my guess, the explanation code is as follows:
From what we see, when I assign a value to the name, it actually changes the value of the array of args, and when I change the value of the arguments, I actually modify the "internal array"
Values, by similar means to achieve the synchronization mechanism we see above, draw a diagram as follows:
2. Length Property
If you know the above principle, then this good guess, either get is true "internal array" length, or to take the pseudo-class arguments length, anyway eventually is
"Internal array" length, right, more commonly used but there is nothing to say.
3.prototype Properties
This is also a function of the internal made out of a property, very interesting, I think we have heard, is not few words can say clearly, ready to put in the next chapter of the detailed talk.
4.caller Properties
Look at this name probably also know a one or two, is used to get the current parent function, do not underestimate this caller oh, did you ever think that it can achieve the function of StackTrace in C #
It? Sometimes we count the JS logs on this thing, like this.
5:call,apply method
Because the functions of both functions are the same, only the call method must be assigned a parameter, and apply must pass the array, if you want to see the real, you can look at their code in VS
Hint, everything is clear, so I put it in a piece to say, but what good is this bird thing? The best thing about it is that you can easily bind objects, and then you can implement a binding object
Dynamic new methods and properties, may be a bit abstract, look at an example is OK.
What we found was that my obj was just an empty object, and after apply, my obj object had the name and age attributes, wouldn't it be magical?
Javascript Spit function