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"); |