<script>
function Person()
{
this.category = 'main';
this.name;
this.age;
}
Person.prototype =
{
setName : function(newName){this.name = newName},
setAge : function(newAge) {this.age = newAge},
getName : function(){return this.name},
getAge : function(){return this.age}
}
function Teacher()
{
this.profeesional = '教師';
}
Teacher.prototype = new Person();
var t = new Teacher();
t.setAge(33);
alert(t.age);
var p = new Person();
alert(p.category)
p.setAge(22);
p.setName('flx');
alert(p.getName());
alert(p.getAge());
</script>
<script>
window.onload = function()
{
function Teacher()
{
this.age = 22;
this.name = 'flx';
this.getName = function(){return this.name};
this.setName = function( newName ){this.name = newName};
this['getAge'] = function(){return 'age'};
this['setAge'] = function(newAge) {this.age = newAge};
}
var t = new Teacher();
alert(t.getName());
alert(t.getAge());
t.setName('lhm');
t.setAge(33);
alert(t.getName());
alert(t.getAge());
}
</script>
function extend(obj,prop)
{
function F(){}
/**
* 當第一次調用exten方法的時候,obj是一個json格式的對象
*/
if(typeof obj == "object") // obj是一個json格式的對象
{
for(var name in obj)
{
F.prototype[name] = obj[name];
}
}else{ //obj是一個函數
F.prototype = obj.prototype; //完成集類屬性的繼承
for(var name in prop)
{
F.prototype[name] = prop[name];
}
}
return F;
}
var Person = extend({
id:5 ,
name : 'flx'
})
var SurPerson = extend(Person,{
sex:'男'
})
var sp = new SurPerson();
alert(sp.name)
alert(sp.id)
alert(sp.sex);