Module mode--attribute-to-method understanding over

Source: Internet
Author: User

The value of the self-executing anonymous function is stored in the variable prison inside var prison = (function () {  var prisoner_name = ' wangming ',      jail_term = ' year term '; 
   //self-executing anonymous function returns an object on which the property is exactly what we want to  return {    prisoner:prisoner_name + '-' + jail_term,    sentence:jail_term  };}) ();//Prison.prisoner_name undefined because it is not a property console.log (Prison.prisoner_name) on the return object of the self-executing anonymous function;//Prison.jail_ Term is undefined because it is not a property on the object returned by the self-executing anonymous function//outputs Undefinedconsole.log (prison.jail_term); Prison is an object so you can still define the Jail_term property on it ... prison.jail_term = ' sentence commuted ';//... But Prison.prisoner is still not updated console.log (prison.jail_term);//This outputs ' WangMing-20 year term ' console.log ( Prison.prisoner);//This outputs ' the term ' console.log (prison.sentence);/*  Prison.prisoner has not been updated for several reasons.  1. Jail_term is not a property on an prison object or prototype, it is an object variable created in the execution environment, the prison variable holds the variable, and the execution environment is no longer present because the function has finished executing.  2. These properties are set only once for anonymous function execution and are never updated. To be able to update them, we have to turn attributes into methods, and each time we call them we will access the variables. */

Module Mode-Property-to-method comprehension

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.