Javascript面向对象编程
//define class var Person = function (name, age){ var _slef = this; //private property _slef.name = name; _slef.age = age; if (typeof Person._initialized == 'undefined') { //private method Person.prototype._eat = function () { console.log('eat....'); } //public method Person.prototype.sayHello = function () { var msg = []; msg.push('welcome,'); msg.push(_slef._name); console.log(msg.join('')); } Person._initialized = true; } //static public method Person.sleep = function () { console.log('sleep...'); } } var Student = function (name,age, number) { var _slef = this; Person.call(_slef,name,age); _slef._number = number; if (typeof Student._initialized == 'undefined') { Student.prototype.sayNumber = function () { var msg = []; msg.push('my name is '); msg.push(_slef.name); msg.push(',my number is '); msg.push(_slef._number); console.log(msg.join('')); } Student._initialized = true; } } Student.prototype = new Person(); var person = new Person('yywang', 21); person.sayHello(); Person.sleep(); var student = new Student('yywang', 12, '2011'); console.dir(student); student.sayNumber(); student.sayHello();
参考:http://w3school.com.cn/js/pro_js_object_defining.asp
http://w3school.com.cn/js/pro_js_inheritance_implementing.asp
----转载请注明出处http://www.cnblogs.com/JerryWang1991/ 谢谢!