面向对象小问答

2017-03-28  本文已影响11人  MGd
1 提供一个构造函数
    function Person() {
//var o = new Object();
//this = o;
// o.__proto__ = Person.prototype;
//o.__proto__.constructor  = Person;
2 在函数内部通过this设置属性和方法
        this.name = "张三";
        this.showName = function () {
            console.log(this.name);
        }
//return o;
    }
3 通过new 函数()创建对象
    var p1 = new Person();
构造函数.prototype
对象.__proto__
Object.getPrototypeOf(对象)
 function Book (name,price,author,press) {
        this.name = name;
        this.price = price;
        this.author = author;
        this.press = press;
    }

    Book.prototype.typeName = "书籍";
    Book.prototype.sellAddress = ["新华书店","京东商城","卓越亚马逊"];
    Book.prototype.read = function () {
        console.log("我的书名为:"+this.name+",作者为"+this.author+"....");
    };

    var b1 = new Book("声名狼藉者的的生活","42.00","福柯","北京大学出版社");
    var b2 = new Book("人性的枷锁","49.00","毛姆","华东师范大学出版社");
上一篇下一篇

猜你喜欢

热点阅读