标签:
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();
标签:
原文地址:http://www.cnblogs.com/alexkn/p/4684619.html