JavaScript creates class objects

Source: Internet
Author: User

The right example:

var person=function (name,age) {this      . Name= ";      This. Age= ";      This. Say=function (msg) {             alert (msg);      }      This.init=function (name,age) {this             . Name=name;             This. age=age;      }      This.init (name,age);      var fun=function () {            alert (' fun ');      }      This.funshow=function () {fun             ();      }
      <pre name= "code" class= "HTML" >      var mutilfun=function () {             <pre name= "code" class= "HTML" >             Say ();
<span style= "font-family:arial, Helvetica, Sans-serif;" > This                          . Say ();</span>
}

      This.mutilfunshow=function () {          <pre name= "code" class= "HTML" >             mutilfun ();
}
Fun ();} Call: Var jon=new person (' Jon ', 26); Jon.say (' Go away '); Alert (jon.name+jon.age), above all to Var shaw=new person (' Shaw ', 26);
   Shaw.fun ();
<pre name= "code" class= "HTML" >   shaw.funshow ();//This is right. Other Shaw is wrong.
   Shaw.mutilfunshow ();//This inside is capable of running to mutilfun this culvert, but <span style= "font-family:arial, Helvetica, Sans-serif;" >mutilfun this number of errors, because Mutilfun in This.say () is not the person in this class This.say (). This here refers to the                                                   </span><span style= "font-family:arial, Helvetica, Sans-serif;" >mutilfun, not person. </span>
Shaw, this object is wrong.

Why Shaw.fun () will be wrong, and the reported error isShaw.fun () is not a function. Because fun is not a property of Shaw, if the attribute is not var;

But Shaw.funshow () is right.

a person is a class and a method, and it is more appropriate to say the method with attributes. Person inside is also step by step execution pull down, first run this.init (name,age), in Run Fun ().



JavaScript creates class objects

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.