Javascript面向对象
//Coder:Zhang Liang
var student1= {
"name":"zhang liang" ,
"age":18 ,
"course":["Math","English" ]
}
//show each property
document.writeln(student1.age);
document.write("<br/>" );
//another display property
document.writeln(student1["age" ]);
document.write("<br/>" );
//add a new property
student1.classmate="Yu Gang" ;
document.writeln(student1.classmate);
document.write("<br/>" );
//get the every propery in object
for(var property in student1)
{
document.write(property+"==>"+ student1[property]);
document.write("<br/>" );
}
//function is also object
var Teacher= function(name)
{
document.writeln("this is my name: "+ name);
document.write("<br/>" );
}
Teacher("Zhang Liang" );
//add a property to function
Teacher.age=38 ;
document.writeln("my age is "+ Teacher.age);
document.write("<br/>" );
//function can be used as a parameter
var Write= function(str)
{
document.writeln(str);
document.write("<br/>" );
}
var ShowName= function(name,func)
{
func(name);
}
ShowName("Huang Hua Xian" ,Write);
//functions as return values
var Plus= function()
{
return function(left,right)
{
return left+ right;
}
}
var GetSum= Plus();
Write(GetSum(7,8 ));
//functions stored as array elements
var array= [];
array[0]= Write;
array[1]="Huang Shu Fang" ;
array[0](array[1 ]);
// there isn't concept "Class" in javascript,only for Construtor
//when there is a this property in function,the function is a Construtor
var Dog= function(name)
{
this.name= name;
this.responseTo=Write("Hello,"+this .name);
}
// var Kitty=new Dog("Kitty");
// Kitty.responseTo();
//static property and function
var Employee= function(name)
{
this.name= name;
}
Employee.Prompt= function()
{
alert("Yes,you are ok!" );
}
Employee.Count=0;//static property
Employee.Prompt();
alert(Employee.Count);
//private property
var Master= function(name)
{
var _name=name;//_name is a private property
this.Name= _name;
}
var Terry=new Master("Terry" );
Write(Terry.Name);
//toString
var Pet= function(firstName,secondName)
{
this.firstName= firstName;
this.secondName= secondName;
}
Pet.prototype.toString = function()
{
return this.firstName+" "+this .secondName;
}
var me=new Pet("Terry","Joen" );
Write(me);
// inherit
//JavaScript 中的函数是对象。每个函数对象都有一个名为 call 的方法,它将函数作为第一个参数的方法进行调用。就是说,作为函数第一个参数传递给 call 的任何对象都将在函数调用中成为“this”的值。
var Cat= function(firstName,secondName,age)
{
Pet.call(this,firstName,secondName);//think Dog : base(name)
this.age= age;
}
Cat.prototype=new Pet();// this makes Cat.prototype inherits from Pet.prototype
// remember that Pet.prototype.constructor points to Pet. We want our Dog instances'constructor to point to Cat.
Cat.prototype.constructor= Cat;
//overide toString()
Cat.prototype.toString= function()
{
return this.firstName+" "+this.secondName+"_"+this .age;
}
var cat=new Cat("MiMi",".T",18 );
Write(cat);
//namespace
var System= {};
System.IO= {};
System.IO.Console= function(name){};
System.IO.Console.prototype.Write= function(str)
{
document.write(str);
document.write("<br/>" );
}
var console=new System.IO.Console();
console.Write("Hello,Zhang Liang!" );
var student1= {
"name":"zhang liang" ,
"age":18 ,
"course":["Math","English" ]
}
//show each property
document.writeln(student1.age);
document.write("<br/>" );
//another display property
document.writeln(student1["age" ]);
document.write("<br/>" );
//add a new property
student1.classmate="Yu Gang" ;
document.writeln(student1.classmate);
document.write("<br/>" );
//get the every propery in object
for(var property in student1)
{
document.write(property+"==>"+ student1[property]);
document.write("<br/>" );
}
//function is also object
var Teacher= function(name)
{
document.writeln("this is my name: "+ name);
document.write("<br/>" );
}
Teacher("Zhang Liang" );
//add a property to function
Teacher.age=38 ;
document.writeln("my age is "+ Teacher.age);
document.write("<br/>" );
//function can be used as a parameter
var Write= function(str)
{
document.writeln(str);
document.write("<br/>" );
}
var ShowName= function(name,func)
{
func(name);
}
ShowName("Huang Hua Xian" ,Write);
//functions as return values
var Plus= function()
{
return function(left,right)
{
return left+ right;
}
}
var GetSum= Plus();
Write(GetSum(7,8 ));
//functions stored as array elements
var array= [];
array[0]= Write;
array[1]="Huang Shu Fang" ;
array[0](array[1 ]);
// there isn't concept "Class" in javascript,only for Construtor
//when there is a this property in function,the function is a Construtor
var Dog= function(name)
{
this.name= name;
this.responseTo=Write("Hello,"+this .name);
}
// var Kitty=new Dog("Kitty");
// Kitty.responseTo();
//static property and function
var Employee= function(name)
{
this.name= name;
}
Employee.Prompt= function()
{
alert("Yes,you are ok!" );
}
Employee.Count=0;//static property
Employee.Prompt();
alert(Employee.Count);
//private property
var Master= function(name)
{
var _name=name;//_name is a private property
this.Name= _name;
}
var Terry=new Master("Terry" );
Write(Terry.Name);
//toString
var Pet= function(firstName,secondName)
{
this.firstName= firstName;
this.secondName= secondName;
}
Pet.prototype.toString = function()
{
return this.firstName+" "+this .secondName;
}
var me=new Pet("Terry","Joen" );
Write(me);
// inherit
//JavaScript 中的函数是对象。每个函数对象都有一个名为 call 的方法,它将函数作为第一个参数的方法进行调用。就是说,作为函数第一个参数传递给 call 的任何对象都将在函数调用中成为“this”的值。
var Cat= function(firstName,secondName,age)
{
Pet.call(this,firstName,secondName);//think Dog : base(name)
this.age= age;
}
Cat.prototype=new Pet();// this makes Cat.prototype inherits from Pet.prototype
// remember that Pet.prototype.constructor points to Pet. We want our Dog instances'constructor to point to Cat.
Cat.prototype.constructor= Cat;
//overide toString()
Cat.prototype.toString= function()
{
return this.firstName+" "+this.secondName+"_"+this .age;
}
var cat=new Cat("MiMi",".T",18 );
Write(cat);
//namespace
var System= {};
System.IO= {};
System.IO.Console= function(name){};
System.IO.Console.prototype.Write= function(str)
{
document.write(str);
document.write("<br/>" );
}
var console=new System.IO.Console();
console.Write("Hello,Zhang Liang!" );