设为首页 加入收藏

TOP

浅谈JavaScript的面向对象思想(五)
2017-09-19 14:21:04 】 浏览:6494
Tags:浅谈 JavaScript 面向 对象 思想
nction (age) {
        this._age = age;
    }});
console.log(Object.getOwnPropertyNames(person));//["name", "_age", "age"]
console.log(Object.getOwnPropertyDescriptor(person,"age"));//{enumerable: false, configurable: false, get: function, set: function}
 


对于数据属性,可以取得:configurable,enumberable,writable和value;


对于访问器属性,可以取得:configurable,enumberable,get和set;


继承机制实现


对象冒充



function Father(name) {
    this.name = name ;
    this.getName = function () {
        return this.name;
    }
 }
function Son(name,age) {
    this._newMethod = Father;
    this._newMethod(name);
    delete  this._newMethod;


    this.age = age;
    this.getAge = function () {
        return this.age;
    }
 }
var father = new Father("Tom");
var son = new Son("Jack",18);
console.log(father.getName());//Tom
console.log(son.getName());//Jack//继承父类getName()方法
console.log(son.getAge());//18
 


多继承(利用对象冒充可以实现多继承)



function FatherA(name) {
    this.name = name ;
    this.getName = function () {
        return this.name;
    }
 }
function FatherB(job) {
    this.job = job;
    this.getJob = function () {
        return this.job;
    }
 }
function Son(name,job,age) {
    this._newMethod = FatherA;
    this._newMethod(name);
    delete  this._newMethod;
    this._newMethod = FatherB;
    this._newMethod(job);
    delete  this._newMethod;


    this.age = age;
    this.getAge = function () {
        return this.age;
    }
 }
var fatherA = new FatherA("Tom");
var fatherB = new FatherB("Engineer");
var son = new Son("Jack","Programmer",18);
console.log(fatherA.getName());//Tom
console.log(fatherB.getJob());//Engineer
console.log(son.getName());//Jack//继承父类FatherA的getName()方法
console.log(son.getJob());//Programmer//继承父类FatherB的getJob()方法
console.log(son.getAge());//18
 


 


call()方法



function Father(name) {
    this.name = name ;
    this.getName = function () {
        return this.name;
    }
 }
function Son(name,job,age) {
    Father.call(this,name);


    this.age = age;
    this.getAge = function () {
        return this.age;
    }
 }
var father = new Father("Tom");
var son = new Son("Jack","Programmer",18);
console.log(father.getName());//Tom
console.log(son.getName());//Jack//继承父类getName()方法
console.log(son.getAge());//18
 


 


多继承(利用call()方法实现多继承)



function FatherA(name) {
    this.name = name ;
    this.getName = function () {
        return this.name;
    }
 }
function FatherB(job) {
    this.job = job;
    this.getJob = function () {
        return this.job;
    }
 }
function Son(name,job,age) {
    FatherA.call(this,name);
    FatherB.call(this,j

首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java同步锁的正确使用 下一篇JSON字符串与JSON对象的相互转换

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目