JS 面向对象
面向对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>JS面向对象</title> </head> <body> <script> //var person = { // name: "jack", // age: 30, // eat: function () { // alert("can eat"); // } //} //alert(person.name); </script> <script> //function Person() { //} //Person.prototype = { // name: "jack", // age: 30, // eat: function () { // alert("can eat"); // } //} //var p = new Person(); //p.eat(); </script> <script> //function People(name) { // this._name = name; //} //People.prototype.say = function () { // alert("peo-hello" + this._name); //} //function Student(name) { // this._name = name; //} //Student.prototype = new People(); //var superSay = Student.prototype.say; //Student.prototype.say = function () { // superSay.call(); // alert("stu-hello" + this._name); //} //var s = new Student("jack"); //s.say(); </script> <script> //(function () { // var n = "n"; // function People(name) { // this._name = name; // } // People.prototype.say = function () { // alert("peo-hello " + this._name + " " + n); // } // window.People = People; //}()); //(function () { // function Student(name) { // this._name = name; // } // Student.prototype = new People(); // var superSay = Student.prototype.say; // Student.prototype.say = function () { // superSay.call(this); // alert("stu-hello " + this._name); // } // window.Student = Student; //}()) //var s = new Student("jack"); //s.say(); </script> <script> //function Person(name) { // var _this = {}; // _this._name = name; // _this.sayHello = function () { // alert("p hello " + _this._name); // } // return _this; // } // function Teacher(name) { // var _this = Person(name); // var surperSay = _this.sayHello; // _this.sayHello = function () { // surperSay.call(_this); // alert("t hello " + _this._name); // } // return _this; // } // var t = Teacher("jack"); // t.sayHello(); </script> <script> (function () { var n = "n"; function Person(name) { var _this = {}; _this._name = name; _this.sayHello = function () { alert("p hello " + _this._name + " " + n); } return _this; } window.Person = Person; }) function Teacher(name) { var _this = window.Person(name); var surperSay = _this.sayHello; _this.sayHello = function () { surperSay.call(_this); alert("t hello " + _this._name); } return _this; } var t = Teacher("jack"); t.sayHello(); </script> </body> </html>