Home >Web Front-end >JS Tutorial >A brief introduction to instance objects, class objects, and local variables (local functions) in js functions

A brief introduction to instance objects, class objects, and local variables (local functions) in js functions

怪我咯
怪我咯Original
2017-07-04 15:11:521378browse

The following editor will bring you a brief discussion of the examples objects, class objects, and local variables(local functions) in jsfunction. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look

Definition

function Person(national,age)
    {
      this.age = age;  //实例对象,每个示例不同
      Person.national = national;  //类对象,所用实例公用
      var bb = 0; //局部变量,外面不能访问(类似局部函数)
    }

Call

var p = new Person("中国", 29);
      document.writeln("age:" + p.age);
      document.writeln("object national:" + p.national);
      document.writeln("Class national:" + Person.national);
      document.writeln("local var:" + p.bb);

      var p2 = new Person("美国", 31);
      document.writeln("</br>");
      document.writeln("age:" + p2.age);
      document.writeln("object national:" + p2.national);
      document.writeln("Class national:" + Person.national);
      document.writeln("local var:" + p2.bb);

      document.writeln("</br>");
      document.writeln("Class national:" + Person.national);
      //age:29 object national:undefined Class national:中国 local var:undefined 
      //age:31 object national:undefined Class national:美国 local var:undefined 
      //Class national:美国

The above is the detailed content of A brief introduction to instance objects, class objects, and local variables (local functions) in js functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn