javascript面向对象整理

面向对象:

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;
}());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泠泠在路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值