构造函数创建对象和Object.create()实现继承

第一个方法用构造函数创建对象,实现方法的继承

/*创建一个对象把所有公用的属性和方法,放进去*/
            function Person() {
                this.name = "W3cplus";
                this.age = 5;
                this.walk = function () {
                    console.log("一个前端网站...");
                };
            };
            /*不是公用的属性另外添加*/
            Person.prototype.sayHello = function () {
                console.log("hello");
            };
            var person1=new Person();
                person1.name="Tome";
                console.log(person1);
                person1.sayHello();
            var nameWvalue=person1['name'];/*获取属性的值*/
                console.log(nameWvalue);

第二个方法Object.create实现类式继承

function Shape() {
              this.x = 0;
              this.y = 0;
            };        
            Shape.prototype.move = function(x, y) {
                this.x += x;
                this.y += y;
                console.info("Shape moved.");
            };            
            function Rectangle() {
              Shape.call(this); 
            }
            Rectangle.prototype = Object.create(Shape.prototype);
            var rect = new Rectangle();
            rect.move(1, 1); //Outputs, "Shape moved."
原文地址:https://www.cnblogs.com/binmengxue/p/5320200.html