当前位置:首页 > 开发教程 > js/jQuery教程 >

JS面向对象教程(2)

时间:2013-04-25 10:40 来源:网络整理 作者:采集侠 收藏

var Person = Object.create(null); Object.defineProperties ( Person, { name : { value: Chen Hao}, email : { value : haoel@hotmail.com }, website: { value: } } ); Person.sayHello = function () { var he

var Person = Object.create(null); 
  
Object.defineProperties 

    Person, 
    { 
        'name'  : {  value: 'Chen Hao'}, 
        'email'  : { value :
'haoel@hotmail.com'}, 
        'website': { value: ''} 
    } 
); 
  
Person.sayHello = function () { 
    var hello = "<p>Hello, I am "+ this.name  + ", <br>" + 
                "my email is: " + this.email + ", <br>" + 
                "my website is: " + this.website; 
    document.write(hello + "<br>"); 

  
var Student = Object.create(Person); 
Student.no = "1234567"; //学号 
Student.dept = "Computer Science"; //系 
  
//使用Person的属性 
document.write(Student.name + ' ' + Student.email + ' ' + Student.website +'<br>'); 
  
//使用Person的方法 
Student.sayHello(); 
  
//重载SayHello方法 
Student.sayHello = function (person) { 
    var hello = "<p>Hello, I am "+ this.name  + ", <br>" + 
                "my email is: " + this.email + ", <br>" + 
                "my website is: " + this.website + ", <br>" + 
                "my student no is: " + this. no + ", <br>" + 
                "my departent is: " + this. dept; 
    document.write(hello + '<br>'); 

//再次调用 
Student.sayHello(); 
  
//查看Student的属性(只有 no 、 dept 和 重载了的sayHello) 
document.write('<p>' + Object.keys(Student) + '<br>');


js/jQuery教程阅读排行

最新文章