开发者

Classical vs Prototypal... how are they so different?

for example in PHP

class foo{
  function foo($name){ //constructor
    $this->name=$name;
  }
  function sayMyName(){
     return $this->name;
  }
}
class bar extends foo{
  function sayMyName(){
     return "subclassed ".$this->name;
  }
}

And in JS

function foo(name){
  this.name=name;
}
foo.prototype.sayMyName=function开发者_C百科(){return this.name};

function bar(){}

bar.prototype=new foo();
bar.prototype.sayMyName=function(){return "subclassed "+this.name};

I am new to javascript, so please enlighten me, Aren't they functionally identical, or am I missing something huge?

If they are identical, how is classical different from prototypal?

thanks in advance...


In JavaScript you can change the inheritance as the program is running, something you cannot do in classical programming. For example:

function foo(name){
  this.name=name;
}
foo.prototype.sayMyName=function(){return this.name};

function foo2(name){
  this.name = name;
}
foo2.prototype.sayMyName = function(){return "My name is "+this.name;};

function bar(){}

bar.prototype = new foo();
var myBar = new bar();
myBar.name = "Marius";
alert(myBar.sayMyName());

bar.prototype = new foo2();
var myBar2 = new bar();
myBar2.name = "Marius";
alert(myBar2.sayMyName());


JavaScript (as you rightly point out) uses a specific type of OOP that is prototype based. In JavaScript you don't create classes, you simply clone existing objects and augmenting them by means of adding members to their prototypes.

In PHP you are creating classes that are blueprints for new objects.


I vouch for Andrew Hare and Marius. It's all about being able to change what a class can do by modifying it at run-time. Classical OOP does not let you do that, you have to specify exactly what a class/object can do and what data properties it has before you compile or before it is interpreted (if an interpreted language).

The code you provided is functionally identical but that's only because you have not taken advantage of prototypes in JS.


Class based inheritance

  • Class and instance are distinct entities.
  • Define a class with a class definition; instantiate a class with constructor methods.
  • Create a single object with the new operator.
  • Construct an object hierarchy by using class definitions to define subclasses of existing classes.
  • Inherit properties by following the class chain.
  • Class definition specifies all properties of all instances of a class. Cannot add properties dynamically at run time.

Prototype based inheritance

  • All objects are instances.
  • Define and create a set of objects with constructor functions.
  • Create a single object with the new operator.
  • Construct an object hierarchy by assigning an object as the prototype associated with a constructor function.
  • Inherit properties by following the prototype chain.
  • Constructor function or prototype specifies an initial set of properties. Can add or remove properties dynamically to individual objects or to the entire set of objects.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜