Define objects in Javascript

Source: Internet
Author: User
Define objects in Javascript

VaRXiaoxing = {grade: 1, Name:"Xiaoxing", Age: 27, sex:"Male", Speak:Function(Words) {alert (This. Name +"Said :"+ Words +"! ") ;}, Improve:Function(){This. Grade ++ ;}}


In this way, I become an object. You can let me say a word to you.


Xiaoxing. Speak ("Welcome to the supersun sky of cnblogs");While(I have friends) {Xiaoxing. Improve ();}

II
It is not object-oriented. The best practice is to use a function to define an object. In this way, the prototype object is used to define its attributes and events to facilitate inheritance and extension.


<Script Language ="JavaScript" Type ="Text/JavaScript">// Create a prototype object, define attributes, methods, and Object events.Student. Prototype = {_ name:Null, _ Age:Null, _ Sex:Null, Showname:Function() {Alert ("Name :"+This. _ Name +"/N"+"Age :"+This. _ Age +"/N"+"Sex :"+This. _ Sex );}}// Use a function to initialize an object.FunctionStudent (name, age, sex ){This. _ Name = Name;This. _ Age = age;This. _ Sex = sex ;}VaRStudent =NewStudent ("Young", 25,"Male");// InstantiateStudent. showname ();// Call the object Method</Script>

<Script language = "JavaScript" type = "text/JavaScript"> // create a prototype object, define attributes, methods, and Object events. Student. prototype = {_ name: NULL, _ age: NULL, _ sex: NULL, showname: function () {alert ("name:" + this. _ name + "/N" + "Age:" + this. _ age + "/N" + "Sex:" + this. _ sex) ;}// a function is used to initialize an object. Function student (name, age, sex) {This. _ name = Name; this. _ age = age; this. _ sex = sex;} var student = new student ("young", 25, "male"); // instantiate student. showname (); // call the object method </SCRIPT>

3.


<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"><Html> <Head> <Title>New Document</Title> <Meta Name ="Generator" Content ="Editplus"> <Meta Name ="Author" Content =""> <Meta Name ="Keywords" Content =""> <Meta Name ="Description" Content =""> </Head> <Body> <Script Language ="JavaScript" Type ="Text/JavaScript">FunctionStudent (name, age, sex ){This. _ Name = Name;This. _ Age = age;This. _ Sex = sex;This. Showname =Function() {Alert ("Name :"+This. _ Name +"/N"+"Age :"+This. _ Age +"/N"+"Sex :"+This. _ Sex );};}VaRStudent =NewStudent ("Young", 25,"Male");// InstantiateStudent. showname ();// Call the object Method</Script> </Body> </Html>

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> <HTML> Thu


<Script Type ="Text/JavaScript">VaRPerson =NewFunction ();// Or var person = function () {}Person. Prototype = {grade: 0, age: 0, sex:Null, Name:Null, Speak:Function(Words) {alert (This. Name +"Said :"+ Words +"! ") ;}, Init:Function(_ Grade, _ age, _ sex, _ name ){This. Grade = _ grade;This. Age = _ age;This. Sex = _ sex;This. Name = _ name ;}}VaRXiaoxing =NewPerson (); Xiaoxing. INIT ("10","27","Male","Xiaoxing"); Xiaoxing. Speak ("Hello everybody");</Script>

<SCRIPT type = "text/JavaScript"> var person = new function (); // or var person = function () {} can both be person. prototype = {grade: 0, age: 0, sex: NULL, name: NULL, speak: function (words) {alert (this. name + "said:" + words + "! ") ;}, Init: function (_ grade, _ age, _ sex, _ name) {This. grade = _ grade; this. age = _ age; this. sex = _ sex; this. name = _ name ;}}var Xiaoxing = new person (); Xiaoxing. init ("10", "27", "male", "Xiaoxing"); Xiaoxing. speak ("Hello everybody"); </SCRIPT>

Here, init is used to initialize and define an object. Of course, we can also write this in function () {}.


var person = function(_grade,_age,_sex,_name){this.grade = _grade;this.age=_age;this.sex=_sex;this.name=_name;}


In this way, we can write the following code:


VaRXiaoxing =NewPerson ("10","27","Male","Xiaoxing"); Xiaoxing. Speak ("Hello everybody");

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.