Deep understanding of JavaScript functions

Source: Internet
Author: User

The key to understanding JavaScript functions is to discard the notion of "class" in the mainstream object-oriented language.

The main object-oriented language, such as C + +, first write "class" definition. When the object is needed, use the new keyword to draw a piece of memory to assign the initial value, there is an object ("Class" "instance"). The source of the confusion is that the JavaScript language mimics object-oriented syntax, and it's hard to keep people from associating traditional object-oriented concepts with keywords like object,new.

We can imitate the concept of "class" gracefully, but always remind ourselves that JavaScript has only objects and no classes.

Animal is an object Animal = {Createnew:function (bundle) {var Animal = {}; var protect = Bundle | |     
        {};//bundle Pass is the pointer, modify protect when the outside object will be changed (unless 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 will be modified, then return blank object {} to Cat protect.sound = Mysound;     
    Cat.meow = function () {return protect.makesound ();};//cat to call method return Cat in protect;     
//javascript is case-sensitive and constructs a new object assignment to cat var cat = cat.createnew ("meow!") by a Cat object; PT ("Cat.sound");//cat cannot directly access sound pt ("Cat.meow ()");//access to sound var bigcat = cat.createnew ("meow!meow!meow!") via function     
; PT ("Bigcat.sound");//bigcat also cannot directly access sound PT ("Bigcat.meow ()");//access to sound via function 

Debugging information:

Cat.sound undefined

Cat.meow () meow!

Bigcat.sound undefined

Bigcat.meow () meow!meow!meow!

[note] If you want the cat (cat) to have a public attribute, because cat itself is an object, you can define the object as a public property in cat and add a function to manipulate the public property in CreateNew. Bigcat

The idea of imitating "class" reminds me of parody's allusion. East Shi imitate Xi Shi elegant behavior certainly cannot reach the standard of Xi Shi, but East Shi don't have their own expertise? For example, will housework be more capable than Xi Shi? The best thing about JavaScript functions is that you can assign values to variables, so we can write "function functions", or we can pass functions as arguments, and functions have their own methods, and so on. Remember when learning early structured programming languages (such as Fortran and C), being corrected to not pass functions as arguments?

Function.prototype.run=function () {return     
    "run~~";     
}     
PT ("Cat.meow.run ()");//functions can also have methods

Debugging information:

Cat.meow.run () run~~

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.