JavaScript对象学习笔记
2010-03-02 23:09 爱研究源码的javaer 阅读(183) 评论(0) 编辑 收藏 举报参考文章 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是只读的