<script>
function Star(uname, age){
this.uname = uname;
this.age = age;}
Star.prototype.sing =function(){
console.log('hi');}//hhh.sex = 1; 定义实例的sex,结果将会得到1//Star.prototype.sex = 0; //实例的sex不存在,将会沿着原型链向上查找,结果得到0
Object.prototype.sex =2;//如果Star.prototype.sex也不存在,继续腌着原型链查找,结果得到2//如果Object.prototype.sex也不存在,结果将得到undefined
var hhh = new Star('hhh',18);
console.log(hhh.sex);//如果实例的sex和Star.prototype.sex同时存在,将根据就近原则得到实例中的sex</script>
10.原型对象中的this指向
<script>
function Star(uname, age){
this.uname = uname;
this.age = age;}
var that;
Star.prototype.sing =function(){
console.log('hi');
that = this;}
var hhh = new Star('hhh',18);
hhh.sing();
console.log(that === this);</script>
在构造函数中,this指向的是对象实例hhh 原型对象里的this指向的也是实例对象hhh
11.利用原型对象扩展内置对象方法
<script>//console.log(Array.prototype); 没有sum方法
Array.prototype.sum =function(){
var sum =0;for(var i =0; i < this.length; i++)
sum += this[i];return sum;}
var arr =[1,2,3];
console.log(arr.sum());
console.log(Array.prototype);//有sum方法</script>