码迷,mamicode.com
首页 > 编程语言 > 详细

javascript中的面向对象

时间:2015-07-29 00:38:56      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

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();

 

 

javascript中的面向对象

标签:

原文地址:http://www.cnblogs.com/alexkn/p/4684619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!