javascript面向对象整理
来源:互联网 发布:java 服务器文件同步 编辑:程序博客网 时间:2024/06/05 03:42
面向对象:
1. 一切事物皆是对象
2. 对象具有封装和继承特性
3. 信息隐藏,(类,属性,方法的隐藏)
一、基本面向对象
直接定义对象:
//定义var person={name:"泠泠在路上",age:22eat:function(){alert('能吃');}}//调用alert(person.name);
二、函数构造器构造对象
构造器构造对象
//定义function person(){}person.prototype={name:"泠泠在路上",age:22,eat:function(){alert('能吃');}}//调用var p = new person();alert(p.name);alert(p.age);alert(p.eat());
三、深入JavaScript面向对象
最简单的事例:
//定义peoplefunction people(){}//创建say方法people.prototype.say = function() { alert("hello");};//定义studentfunction student(){}//student继承peoplestudent.prototype=new people();//new studentvar s = new student();//调用s.say();//执行结果//弹出hello
下面加深理解,给student添加say方法,看看会出现什么情况?
//定义peoplefunction people(){}//创建say方法people.prototype.say = function() { alert("hello");};//定义studentfunction student(){}//student继承peoplestudent.prototype=new people();//student创建say方法student.prototype.say= function(){ alert("student hello");}//new studentvar s = new student();//调用s.say();//执行结果://弹出student hello
由此可见,子类覆盖了父类的say方法,那么,假如这种情况下,我就是要调用父类的say方法,那又该怎么办呢?
//定义peoplefunction people(){}//创建say方法people.prototype.say = function() { alert("hello");};//定义studentfunction student(){}//student继承peoplestudent.prototype=new people();//定义变量调用父类的say方法var superSay=student.prototype.say;student.prototype.say= function(){//调用 superSay.call(this); alert("student hello");}//new studentvar s = new student();//调用s.say();//执行结果//先弹出hello,后弹出student hello
接下来,我们给方法传入参数,看看会有什么神奇的效果~~
值得注意的是,父类中的参数,在子类中也需要指定它的参数。
//定义peoplefunction people(name){ this._name=name;}//创建say方法people.prototype.say = function() { alert("people hello"+this._name);};//定义studentfunction student(name){ this._name=name;}//student继承peoplestudent.prototype=new people();//定义变量调用父类的say方法var superSay=student.prototype.say;student.prototype.say= function(){//调用 superSay.call(this); alert("student hello"+this._name);}//new studentvar s = new student("泠泠在路上");//调用s.say();//执行结果//先弹出people hello泠泠在路上,后弹出student hello泠泠在路上
下面,使用JavaScript实现信息的隐藏,
function person() { //定义空对象存储数据 var _this={} _this.sayHello=function () { alert("person hello"); } return _this;}function teacher() {//_this获得person所有数值 var _this=person(); return _this;}var t=teacher();t.sayHello();//执行结果//弹出person hello
假如teacher再定义sayhello方法,又会出现什么呢?
function person() { //定义空对象存储数据 var _this={} //sayHello方法 _this.sayHello=function () { alert("person hello"); } return _this;}function teacher() { var _this=person(); //sayHello方法 _this.sayHello=function () { alert("teacher hello"); } return _this;}var t=teacher();t.sayHello();//执行结果//弹出teacher hello
如果又想要获得父类方法的值,又该如何处理?需要通过赋值的方式来进行处理。
类似的,只需要在teacher方法中写入下面部分即可。
var superSay=_this.sayHello(); //sayHello方法 _this.sayHello=function () { superSay.call(_this); alert("teacher hello"); } return _this;
最后,传参数的部分与上面类似,
function person(name) { //定义空对象存储数据 var _this={} _this._name=name; //sayHello方法 _this.sayHello=function () { alert("person hello"+_this._name); } return _this;}function teacher(name) { var _this=person(name); var superSay=_this.sayHello(); //sayHello方法 _this.sayHello=function () { superSay.call(_this); alert("teacher hello"+_this._name); } return _this;}var t=teacher("泠泠在路上");t.sayHello();//执行结果//先弹出person hello泠泠在路上,后弹出teacher hello泠泠在路上
最后看一下封装变量的话,怎么处理?
将函数放在一个闭包里执行。
(function () { //封装的变量 var n='ime'; function person(name) { //定义空对象存储数据 var _this={} _this._name=name; //sayHello方法 _this.sayHello=function () { alert("person hello"+_this._name+n); } return _this; } //往外传执行 window.person=person;}());
阅读全文
0 0
- javascript 面向对象整理
- javascript面向对象整理
- JavaScript 面向对象整理笔记(一)
- JavaScript 面向对象整理笔记(二)
- javaScript 面向对象-对象
- 面向对象整理二
- 面向对象知识点整理
- 面向对象整理二
- 面向对象链接整理
- 面向对象知识点整理
- 面向对象笔记整理
- javascript 面向对象初探
- JavaScript 面向对象编程
- JavaScript 面向对象编程
- 面向对象的javascript
- javascript面向对象编程
- javascript的面向对象
- JavaScript的面向对象
- priority_queue 建立最小堆
- 图片上传
- 范凌:设计与人工智能行业报告与商业实践(上)
- GreenDao3.2在AndroidStudio中的配置和初步使用
- 两个小效果(美女系列..)1.纯css和css3实现;2.jquery实现.
- javascript面向对象整理
- LVS NAT/DR/TUN三种工作模式详解(转)
- 自定义ImageView实现自动放大缩小动画
- 并发方式 之io多路复用
- 启动Tomcat报了一个异常Error starting static Resource
- phpmyadmin中创建一个存储过程或函数,提示出错怎么办?
- [置顶] [Centos6.5]MongoDB安装以及php_mongo扩展的安装
- 如何成为一个高效的设计师
- MYSQL之事务隔离级别与锁的关系