Javascript面向对象

1 篇文章 0 订阅
1 篇文章 0 订阅

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!"
);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值