面向对象:
1. 一切事物皆是对象
2. 对象具有封装和继承特性
3. 信息隐藏,(类,属性,方法的隐藏)
一、基本面向对象
直接定义对象:
//定义
var person={
name:"泠泠在路上",
age:22
eat: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面向对象
最简单的事例:
//定义people
function people(){
}
//创建say方法
people.prototype.say = function() {
alert("hello");
};
//定义student
function student(){
}
//student继承people
student.prototype=new people();
//new student
var s = new student();
//调用
s.say();
//执行结果
//弹出hello
下面加深理解,给student添加say方法,看看会出现什么情况?
//定义people
function people(){
}
//创建say方法
people.prototype.say = function() {
alert("hello");
};
//定义student
function student(){
}
//student继承people
student.prototype=new people();
//student创建say方法
student.prototype.say= function(){
alert("student hello");
}
//new student
var s = new student();
//调用
s.say();
//执行结果:
//弹出student hello
由此可见,子类覆盖了父类的say方法,那么,假如这种情况下,我就是要调用父类的say方法,那又该怎么办呢?
//定义people
function people(){
}
//创建say方法
people.prototype.say = function() {
alert("hello");
};
//定义student
function student(){
}
//student继承people
student.prototype=new people();
//定义变量调用父类的say方法
var superSay=student.prototype.say;
student.prototype.say= function(){
//调用
superSay.call(this);
alert("student hello");
}
//new student
var s = new student();
//调用
s.say();
//执行结果
//先弹出hello,后弹出student hello
接下来,我们给方法传入参数,看看会有什么神奇的效果~~
值得注意的是,父类中的参数,在子类中也需要指定它的参数。
//定义people
function people(name){
this._name=name;
}
//创建say方法
people.prototype.say = function() {
alert("people hello"+this._name);
};
//定义student
function student(name){
this._name=name;
}
//student继承people
student.prototype=new people();
//定义变量调用父类的say方法
var superSay=student.prototype.say;
student.prototype.say= function(){
//调用
superSay.call(this);
alert("student hello"+this._name);
}
//new student
var 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;
}());