基于对象与面向对象有什么区别?

基于对象与面向对象有什么区别?

阅读 3.1k
2 个回答

来这里问的好处是可以避免使用 Baidu,毕竟这里还是有人用 Google 的(逃)

Wikipedia: Object-based

In computer science, the term object-based has two different senses:

A somehow limited version of object-oriented programming, where one or more of the following restrictions applies: (a) There is no implicit inheritance, (b) there is no polymorphism, (c) only a very reduced subset of the available values are objects (typically the GUI components).

Prototype-based systems (that is, those based on "prototype" objects that are not instances of any class).

简要翻译下:

在计算机科学当中,基于对象一词有两种不同含义

  • 满足至少其一:没有明确的继承、没有多态、对象的使用只局限在很小的范围内

  • 基于原型而不是类的实例构建对象


补充一下:

在 ES2015 之前,Javascript 只能基于对象(参见 http://stackoverflow.com/a/6954346/3291805

现在 ES2015 可以真正面向对象了(明确的继承)

class Base {
    foo(bar = '') {
        console.log('foo' + bar);
    }
}

class Example extends Base {
    constructor() {
        super();
        
        this.bar = 'bar';
    }
    foo() {
        super.foo(this.bar);
    }
}

new Base().foo(); // foo
new Example().foo(); // foobar
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题