JavaScript's private variables and methods, common variables and methods, and privileged methods, constructors, static common attributes, and static common methods

Source: Internet
Author: User

A private variable: a property and method defined inside a function is called the function's private property and method common variables: Objects and methods created inside a function called this function are common properties and methods
Privileged methods: a common property and method created through this can access both the function and the private properties and methods of the function called the privileged method
Constructor: The object invokes the privileged method at creation time to initialize the properties of the instance, also known as the constructor
  
 
  1. /**
  2. * 类的内部私有属性、私有方法、特权方法、共有属性和共有方法以及构造器
  3. * @param id
  4. * @param bookname
  5. * @param price
  6. * @constructor
  7. */
  8. var BookDemo = function(id,bookname,price){
  9. /*
  10. *申明在函数内部的属性和方法是该函数对象的私有属性和私有方法
  11. */
  12. var num = 1;// 私有属性
  13. //私有方法
  14. function checkId(id){
  15. return id;
  16. }
  17. /**
  18. * 通过this创建的属性和方法是共有属性和方法 也称为特权方法
  19. */
  20. this.bookname = bookname;
  21. this.getBookName = function(){
  22. console.info(‘getName特权方法,可以调用类的私有属性和方法:num=‘+num);
  23. }
  24. this.setBookName = function(bookname){
  25. console.info(‘setBookName特权方法‘);
  26. }
  27. this.copy = function(){
  28. console.info(‘copy的共有方法‘)
  29. }
  30. /**
  31. * 在对象创建时候调用这些共有方法初始化实例对象的属性,那么这些共有方法又称为构造器,
  32. * 在对象被new出来的时候就会被执行 构造器必须有对应的特权方法的实现,否则就会报错
  33.     * This.setbookname is not a function
  34. */
  35. this.setBookName(bookname); //如果包含方法则该方法会在初始化的时候就会被创建
  36. this.getBookName();
  37. }
To invoke the test method:
 
   
  
  1. var bookDemo = new BookDemo(‘1‘,‘Javascript设计模式‘,‘48.5‘);//由于调用了 this.setBookName 和this.getBookName 属性通过new创建实例的时候就会执行 this.getBookName和 this.setBookName 的特权方法
  2. The result of all outputs is: the Setbookname privileged method and the GetName privileged method, which can invoke the private properties and methods of the class: Num=1
Continue calling test method
 
   
  
  1. console.info(bookDemo.num); //Undefined
Modify Bookdemo to track changes in BookName
  
 
  1. /**
  2. * 类的内部私有属性、私有方法、特权方法、共有属性和共有方法以及构造器
  3. * @param id
  4. * @param bookname
  5. * @param price
  6. * @constructor
  7. */
  8. var BookDemo = function(id,bookname,price){
  9. /*
  10. *申明在函数内部的属性和方法是该函数对象的私有属性和私有方法
  11. */
  12. var num = 1;// 私有属性
  13. //私有方法
  14. function checkId(id){
  15. return id;
  16. }
  17. /**
  18. * 通过this创建的属性和方法是共有属性和方法
  19. */
  20. this.bookname = bookname;
  21. this.getBookName = function(){
  22. console.info(‘getName特权方法,可以调用类的私有属性和方法:num=‘+num+",bookname="+bookname);
  23. }
  24. this.setBookName = function(bookname){
  25. console.info(‘setBookName特权方法,bookname=‘+bookname);
  26. }
  27. this.copy = function(){
  28. console.info(‘copy的共有方法‘)
  29. }
  30. /**
  31. * 在对象创建时候调用这些共有方法初始化实例对象的属性,那么这些共有方法又称为构造器,
  32. * 在对象被new出来的时候就会被执行
  33. */
  34. this.setBookName(bookname);
  35. this.getBookName();
  36. }
Test
  
 
  1. var bookDemo = new BookDemo(‘1‘,‘Javascript设计模式‘,‘48.5‘);
  2. bookDemo.setBookName(‘Java设计模式‘);
  3. /**
  4. * 输出依次为:setBookName特权方法,bookname=Javascript设计模式
  5. * getName特权方法,可以调用类的私有属性和方法:num=1,bookname=Javascript设计模式
  6. * setBookName特权方法,bookname=Java设计模式
  7. */
Static common method and static common property of class
  
 
  1. /***
  2. * 通过new创建对象是,由于类外面通过点语法创建的属性和方法没有执行到,所以新创建的对象无法获取到他们
  3. * 但是可以通过类来获取,所以称之为类静态共有属性和方法
  4. * @type {boolean}
  5. */
  6. BookDemo.isChinese = true; //类静态共有属性
  7. //类静态共有方法
  8. BookDemo.checkBookName = function(){
  9. console.info(‘通过点方法创建的类静态共有方法,只能改类自己直接方法,不能通过new创建对象来访问‘);
  10. }
  11. /**
  12. * 通过prototype创建的属性和方法可以通过this来访问,所以通过prototype创建的属性和方法称之为共有属性和方法
  13. * @type {{isJsBook: boolean, display: BookDemo.display}}
  14. */
  15. BookDemo.prototype = {
  16. isJsBook : true,
  17. display : function(){
  18. console.info(‘外部通过prototype创建的方法可以访问该函数内部的属性,bookname=‘+this.bookname);
  19. }
  20. }
Test
 
   
  
  1. < Span class= "PLN" >console info ( bookdemo ischinese //undefined
  2. bookdemo checkbookname (); //the class static common method created by the point method, can only be changed to the class itself direct method, cannot create object through new to access
  3. Bookdemo display (); //the properties inside the function can be accessed through a method created by prototype, bookname=undefined



From for notes (Wiz)

JavaScript's private variables and methods, common variables and methods, and privileged methods, constructors, static common attributes, and static common methods

Related Article

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.