javascript中的面向对象

javascript没有类,但有对象。下面是javascript面向对象时调用的三种办法。

第一种:

function Student(name){
  this.name = name;
  this.hello = function(){
    alert('hello, '+this.name+'!');
  }
}
var xiaoming = new Student('小明');

xiaoming.name;
xiaoming.hello();

第二种

var Student = {
  name: '',
  hello:function(){
    alert('hello,'+this.name+'!');
  }
}
var s = Student;
s.name = "小明";
s.hello();

第三种

var Student = {
  name: '',
  hello:function(){
    alert('hello,'+this.name+'!');
  }
}

function createStudent(name){
  var s = Object.create(Student);
  s.name = name;
  return s;
}

var xiaoming = createStudent('小明');
xiaoming.hello();

 

 
posted @ 2015-07-28 22:56  李雷雷alexkn  阅读(213)  评论(0编辑  收藏  举报