面向对象,函数构造方法
2017-02-05 13:49
225 查看
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>面向对象,函数构造方法1</title> </head> <body> </body> <script> /*(function(){}());将变量方法等搞成不同作用域,即封装某些代码块,后加()是为了可以执行*/ (function(){ var n="局部作用域的变量n"; function people(name,age,sex){//people类的构造函数 this.name=name; this.age=age; this.sex=sex; } people.prototype.n=n; people.prototype.say=function(){ alert("people----say"); } window.people=people;//赋值给window,全局才可以调用 }()); (function(){ function stu(name,age,sex){//stu类的构造函数 this.name=name; this.age=age; this.sex=sex; } stu.prototype=new people();//继承people类 var speple_say=stu.prototype.say; people.prototype.say=function(){ speple_say.call(this);//子类调用父类的say() alert("stu----say");//重写父类say() } window.stu=stu;//赋值给window,全局才可以调用 }()); var p=new people('小明','12','男'); p.say(); alert(p.name+" "+p.n); var s=new stu(); s.say(); alert(s.n); </script> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>面向对象,函数构造方法2</title> </head> <body> </body> <script> /*(function(){}());将变量方法等搞成不同作用域,后加()是为了可以执行*/ (function(){ var n="局部变量n"; function people(name){ var _this={};//声明_this变量是一个空对象 _this.name=name; _this.say=function(){ alert("people----say---"+_this.name); } return _this;//个人觉得people更像是一个方法而不是类 } window.people=people; }()); function stu(name){ var _this=people(name); var supersay=_this.say; _this.say=function(){ supersay.call(_this); alert("stu----say---"+_this.name); } return _this; } var s=stu('jick'); s.say(); </script> </html>
相关文章推荐
- 面向对象----构造方法、this 关键字、函数的参数传递、package语句/import语句
- C++面向对象笔记:构造、析构函数、成员函数
- 面向对象之字段,属性,类,实例,构造方法,重载
- 面向对象构造方法、抽象类、内部类
- 面向对象第五课,方法重载,构造方法的重载
- 面向对象之构造函数(可让程序运行前初始化一些东西)、函数(方法)重载
- OC基础语法<1.2> 入门面向对象语法-构造方法和description
- php面向对象全攻略 (四)构造方法与析构方法
- 面向对象第四课,构造器,构造方法
- 封装回调函数——为对象方法(Object Method,参数中带this指针的函数) 构造 普通函数(参数中无this指针的函数)形式 的入口
- Objective-C 面向对象基础-构造方法
- 第三十三天 面向对象part4 -构造匿名方法
- 面向对象基础知识(2)- 构造方法
- 黑马程序员——面向对象思想、设计类与创建对象、对象与函数、方法与函数、合理设计类
- Java面向对象基础--构造方法私有化
- Java面向对象之构造方法、静态、this关键字
- 黑马程序员--.NET笔记--面向对象基础、构造方法
- java面向对象--this关键字、引用变量、构造方法
- 面向对象基础(类与对象的定义、进一步研究、封装性、构造方法与匿名对象)
- 黑马程序员_面向对象之构造方法