The key to understanding javascript functions is to discard the concept of "Classes" in mainstream object-oriented languages.
Mainstream object-oriented languages, such as C ++, must first define the class. When an object is required, use the new keyword to draw out a piece of memory and assign the initial value to an instance of the object "class "). The source of obfuscation is that the javascript language imitates the object-oriented syntax. When you see keywords such as Object and new, it is difficult to think of traditional object-oriented classic concepts.
We can elegantly imitate the concept of "class", but always remind ourselves that javascript only has objects and no classes.
- // Animal is an object
- Animal = {
- CreateNew: function (bundle ){
- Var animal = {};
- Var protect = bundle | {}; // The bundle transmits a pointer. When protect is modified, the external object will change unless it is not transmitted)
- Protect. sound = 'growl ';
- Protect. makeSound = function (){
- Return protect. sound;
- }
- Return animal;
- }
- }
-
- // Cat is also an object
- Cat = {
- CreateNew: function (mySound ){
- Var protect = {};
- Var cat = Animal. createNew (protect); // protect is modified, and then a blank object {} is returned to cat.
- Protect. sound = mySound;
- Cat. meow = function () {return protect. makeSound () ;}; // cat: Call the method in protect.
- Return cat;
- }
- }
- // Javascript is case sensitive. A new object is constructed using a Cat object and assigned to cat.
- Var cat = Cat. createNew ("meow! ");
- Pt ("cat. sound"); // cat cannot directly access the sound
- Pt ("cat. meow ()"); // you can access the sound using a function.
- Var bigCat = Cat. createNew ("meow! Meow! Meow! ");
- Pt ("bigCat. sound"); // bigCat cannot directly access the sound.
- Pt ("bigCat. meow ()"); // you can access the sound using a function.
Debugging information:
Cat. sound undefined
Cat. meow () meow!
BigCat. sound undefined
BigCat. meow () meow! Meow! Meow!
[Note] If you want bigCat to have a common attribute, you can define an object as a common attribute in cat because Cat is also an object, add a function to createNew to manipulate this public attribute.
Imitating the concept of "class" reminds me of the story of "Dong Xiao. East Shi's elegant manners cannot reach the level of east Shi, but does east Shi have no special skills? For example, Will housework be more competent than Xi Shi?The biggest benefit of javascript Functions is that they can be assigned to variables. Therefore, we can write "function functions", or, when passing a function as a parameter, the function can also have its own method, and so on.Do you still remember being corrected when learning early structured programming languages such as Fortran and C) and saying that functions cannot be passed as parameters?
- Function. prototype. run = function (){
- Return "run ~~ ";
- }
- Pt ("cat. meow. run ()"); // the function can also have a method
Debugging information:
Cat. meow. run () run ~~
The following is a method of cloning a function:
- // This method does not require the prototype keyword to be written in the future.
- Function. prototype. method = function (name, func ){
- This. prototype [name] = func;
- Return this;
- };
- // Add the cloneFunction Method
- Function. method ("cloneFunction", function (){
- Var slice = Array. prototype. slice;
- Var args = slice. apply (arguments );
- Var that = this;
- Return function (){
- Return that. apply (null, args. concat (slice. apply (arguments); // here the meaning of slice. apply (arguments) is different from the previous one.
- }});
- // Define the add function
- Var add = function (a, n ){
- Return a + n;
- }
-
- Pt ("add ()"); // debug the output
-
- Var add1 = add. cloneFunction (1); // clone 1
- Var add2 = add. cloneFunction (2); // clone 2
- Var add3 = add. cloneFunction (3); // clone 3
-
- Pt ("add1 (5)"); // debug the output
- Pt ("add2 (5)"); // debug the output
- Pt ("add3 (5)"); // debug the output
Debugging information:
Add (0, 5) 5
Add1 (5) 6
Add2 (5) 7
Add3 (5) 8
To obtain all executable code, download the attachment.
This article is from the iData blog, please be sure to keep this source http://idata.blog.51cto.com/4581576/1101621