Deep understanding of javascript Functions

Source: Internet
Author: User

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.

 
 
  1. // Animal is an object
  2. Animal = {
  3. CreateNew: function (bundle ){
  4. Var animal = {};
  5. Var protect = bundle | {}; // The bundle transmits a pointer. When protect is modified, the external object will change unless it is not transmitted)
  6. Protect. sound = 'growl ';
  7. Protect. makeSound = function (){
  8. Return protect. sound;
  9. }
  10. Return animal;
  11. }
  12. }
  13.  
  14. // Cat is also an object
  15. Cat = {
  16. CreateNew: function (mySound ){
  17. Var protect = {};
  18. Var cat = Animal. createNew (protect); // protect is modified, and then a blank object {} is returned to cat.
  19. Protect. sound = mySound;
  20. Cat. meow = function () {return protect. makeSound () ;}; // cat: Call the method in protect.
  21. Return cat;
  22. }
  23. }
  24. // Javascript is case sensitive. A new object is constructed using a Cat object and assigned to cat.
  25. Var cat = Cat. createNew ("meow! ");
  26. Pt ("cat. sound"); // cat cannot directly access the sound
  27. Pt ("cat. meow ()"); // you can access the sound using a function.
  28. Var bigCat = Cat. createNew ("meow! Meow! Meow! ");
  29. Pt ("bigCat. sound"); // bigCat cannot directly access the sound.
  30. 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?

 
 
  1. Function. prototype. run = function (){
  2. Return "run ~~ ";
  3. }
  4. 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:

 
 
  1. // This method does not require the prototype keyword to be written in the future.
  2. Function. prototype. method = function (name, func ){
  3. This. prototype [name] = func;
  4. Return this;
  5. };
  6. // Add the cloneFunction Method
  7. Function. method ("cloneFunction", function (){
  8. Var slice = Array. prototype. slice;
  9. Var args = slice. apply (arguments );
  10. Var that = this;
  11. Return function (){
  12. Return that. apply (null, args. concat (slice. apply (arguments); // here the meaning of slice. apply (arguments) is different from the previous one.
  13. }});
  14. // Define the add function
  15. Var add = function (a, n ){
  16. Return a + n;
  17. }
  18.  
  19. Pt ("add ()"); // debug the output
  20.  
  21. Var add1 = add. cloneFunction (1); // clone 1
  22. Var add2 = add. cloneFunction (2); // clone 2
  23. Var add3 = add. cloneFunction (3); // clone 3
  24.  
  25. Pt ("add1 (5)"); // debug the output
  26. Pt ("add2 (5)"); // debug the output
  27. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.