繁体   English   中英

如何在JavaScript中使用继承与构造方法返回具有私有属性的对象文字?

[英]How to Use Inheritance in JavaScript with Constructor Methods Returning Object Literals with Private Properties?

var Animal = function(config) {
    config = config || {};
    var name = config.name,
        numLegs = config.numLegs,
        weight = config.weight,
        speed = config.speed,
        sound = config.sound
    return {
        getName: function () {
            return name;
        },
        getNumLegs: function () {
            return numLegs;
        },
        getWeight: function () {
            return weight;
        },
        getSpeed: function () {
            return speed;
        },
        getSound: function () {
            return sound;
        },
        run: function(distance, unit) {
            unit = unit || 'miles';
            return 'The ' + name + ' ran ' + distance + ' ' + unit;
        },
        speak: function() {
            return 'The ' + name + ' says "' + sound + '"';
        }
    }
};

function DragonFly(config) {
    var me = {},
        numWings = config.numWings;
    me.prototype = new Animal(config);
    me.getNumWings = function() {
        return numWings;
    };
    me.fly = function(distance, unit) {
        unit = unit || 'miles';
        return 'The ' + me.name + ' flew ' + distance + ' ' + unit;
    }
    return me;
}

var dragonFly = new DragonFly({
    numWings: 2,
    name: 'DragonFly',
    numLegs: 6
});

好的,来自PHP背景,我不了解JavaScript中的继承,我想要一些帮助。

基本上,这是我想要使用dragonFly对象的实例:

dragonFly.getName(); // 'DragonFly'
dragonFly.fly(1, 'mile'); // 'The dragonfly flew 1 mile';
dragonFly.run(1, 'yard'); // 'The dragonfly ran 1 yard';

我还想知道如何重写方法并调用那些重写方法的父级。 我的做法有什么问题? 上面的所有示例都返回undefined或抛出错误。 我使用object-literal样式的主要原因是我可以将属性设为私有。

“最快”的方式:

var Animal = function(config) {
config = config || {};
var name = config.name,
    numLegs = config.numLegs,
    weight = config.weight,
    speed = config.speed,
    sound = config.sound
return {
    getName: function () {
        return name;
    },
    getNumLegs: function () {
        return numLegs;
    },
    getWeight: function () {
        return weight;
    },
    getSpeed: function () {
        return speed;
    },
    getSound: function () {
        return sound;
    },
    run: function(distance, unit) {
        unit = unit || 'miles';
        return 'The ' + name + ' ran ' + distance + ' ' + unit;
    },
    speak: function() {
        return 'The ' + name + ' says "' + sound + '"';
    }
   }
};

function DragonFly(config) {
var me = new Animal(config);
var numWings = config.numWings;
me.getNumWings = function() {
    return numWings;
};
me.fly = function(distance, unit) {
    unit = unit || 'miles';
    return 'The ' + me.name + ' flew ' + distance + ' ' + unit;
}
return me;
}

var dragonFly = new DragonFly({
numWings: 2,
name: 'DragonFly',
numLegs: 6
});

你在脚本中混合了两种“继承”,“经典”继承和原型继承,除非你想遇到严重的麻烦,否则你不能这样做。 两者兼顾,都有其优点和缺点。 自从你开始使用它时,坚持“经典”继承或对象扩充。

对象文字没有原型,函数有原型。 这就是为什么在我看来 js不是“真正”面向对象,但它可以模仿面向对象的语言

现在一个很好的练习是尝试使用函数和原型,但我不确定你是否可以创建私有字段。

编辑:me.name应该是me.getName(),因为name是“private”。 我认为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM