js OOP 面向对象

数组对象:

数组也是对象,原型链:
var arr = []
typeof arr // "object"
arr ----> Array.prototype ----> Object.prototype ----> null

函数对象:

函数原型链
foo ----> Function.prototype ----> Object.prototype ----> null

对象:

        function Student(name) {
            this.name = name;
        }
        // 放这共享同一个hello,节约内存
        Student.prototype.hello = function () {
            console.log('Hello, ' + this.name + '!');
        }

        var xiaoming = new Student('小明');
        console.log(xiaoming.name); // '小明'
        xiaoming.hello(); // Hello, 小明!

        console.log(' _____________________ ');

        var xiaohong = new Student('小红');
        console.log(xiaohong.name);
        xiaohong.hello();

        console.log(' _____________________ ');

        console.log(xiaoming.hello); // '小明'
        console.log(xiaohong.hello); console.log(xiaoming.hello == xiaohong.hello); // true

对象原型链

xiaoming ----> Student.prototype ----> Object.prototype ----> null

xiaoming ↘
xiaohong -→ Student.prototype ----> Object.prototype ----> null
xiaojun  ↗

https://www.liaoxuefeng.com/wiki/1022910821149312/1023022043494624

https://www.cnblogs.com/jiejiejy/p/7666091.html 

适合新手

.

猜你喜欢

转载自www.cnblogs.com/xiangsj/p/12327251.html