JavaScript对象学习笔记

参考文章 http://www.cnblogs.com/sanshi/archive/2009/05/07/1454076.html

//function User(name) {

//    this.name = name;
//}

//User.prototype.getname = function() {

//    return this.name;
//}

//var user = new User('Zhang San');

//alert(user.constructor === User) //true

//alert(user.constructor.prototype === User.prototype)//true

//alert({}.constructor === Object) //true

//alert([].constructor === Array) //true

//alert(''.constructor === String)//true

//alert(user.constructor.prototype.constructor);//User

function Person(sex) {

    this.sex = sex;
}

function User(name) {

    this.name = name;
}

User.prototype = new Person('man');

var user = new User('Zhang San');

//alert(user.sex); //'man'

//alert(user.constructor);//Person

//alert(User.prototype.constructor); //Person

//Array.prototype.max = function() {
//    var maxValue = this[0];

//    for (var i = 1; i < this.length; i++) {

//        maxValue = this[i];
//    }
//    return maxValue;

//}

//alert([2, 33, 25].max());//25

Array.prototype = {

    max: function() {
        var maxValue = this[0];

        for (var i = 1; i < this.length; i++) {

            maxValue = this[i];
        }
        return maxValue;
    }

};

alert([2, 33, 25].max());//因为Array.prototype是只读的


 

原文地址:https://www.cnblogs.com/johnwonder/p/1676832.html