//最基本的构造对象 // var person={ // name: "iwen", // age: "30", // eat:function () { // alert("能吃") // } // } // alert(person.name); // //函数构造器构造对象 // function person() { // // } // person.prototype={//用prototype创建对象原型,也就是对象里的东西 // name: "iwen", // age: "30", // eat:function () { // alert("我也能吃") // } // } // var p =new person();//创建person的对象 // //深入了解面向对象,方法1 // (function () { // var n = "ime"; // function people(name) { // this._name = name;//传递了参数,通过this指向调用它的对象 // }//创建了函数把它当作类来使用 // people.prototype.say = function () { // alert("pHello"+this._name+n) // }//指定原型量中的方法 // window.people = people;//给外部公开结构,把它赋给顶级的window窗口 // }());//封装隐藏对象 // function Student(name) { // this._name = name;//父类指定了子类也要指定 // // // } // Student.prototype=new people();//让Student继承people // var surperSay = Student.prototype.say; // Student.prototype.say = function () { // surperSay.call(this);//强制调用 // alert("stu-hello"+this._name);//复写父类的方法,会覆盖 // } // var s = new Student("iwen"); // s.say();//检查一下继承是否成功 //深入了解面向对象,方法2 (function () { function person(name)//传递参数 { var n ="ime" var _this={}//创建一个空的对象 _this._name=name; _this.sayHello=function ()//创建一个方法 { alert("Phello"+_this._name+n) } return _this; } window.person = person; }())//把它弄成一个局域的,可以在里面改变参数 function Teacher(name) { var _this = person(name);//创建对象_this,把person赋值给它 var surperSay = _this.sayHello;//将父类的方法传递给surperSay _this.sayHello=function () { surperSay.call(_this);//通过call强行调用方法的执行 alert("tHello"+_this._name);//子类也想显示,复写了父类的方法,会覆盖父类 } return _this; } var t = Teacher("iwen"); t.sayHello();
JS面向对象
最新推荐文章于 2024-06-11 16:42:32 发布