JavaScript如何实现面向对象和继承机制?

时间:2022-01-27 19:30:32
JavaScript实现面向对象?

 function MyObject()

{

   this.name="myobject";
   this.type="class";

   this.methodA=function()

   {

      alert(this.name);

 }

    this.methodB=function()

   {

     return this.type;

   }

}

var myObject = new MyObject();

myObject.methodA();

var type = myObject .methodB();

alert(ype);

继承机制:

(1)构造继承适合单个class,优点是继承关系明确;

(2)原型链继承,适合无参数继承;

(3)混合继承方式,视情况而定

参考:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html(面向对象)

参考:http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javascript.html