2021年4月16日星期五

关于构造函数和数组原型链上添加构造函数的回顾记录

<!DOCTYPE html><html> <head>  <meta charset="utf-8">  <title></title> </head> <body>  <h1 style="text-align: center;">内容在控制台</h1> </body> <script type="text/javascript">  //创建构造函数  function Test(name){   this.name = name;   this.xbc = '你瞅啥。';   this.hello = function(){    console.log('hello,'+this.xbc);   }   this.like = function(){    console.log(this.name+'喜欢瞅你。');   }  }    var isMe = new Test('XXX');  isMe.hello();  isMe.like();      // isMe.constructor === Test.prototype.constructor; // true  // Test.prototype.constructor === Test; // true  // Object.getPrototypeOf(isMe) === Test.prototype; // true  // isMe instanceof Test; // true  console.log(Test);  console.log(isMe.constructor);  console.log(Test.prototype.constructor);  console.log(Test.prototype);  console.log(Object.getPrototypeOf(isMe));  console.log(isMe instanceof Test);    function Cat(param){   this.name = param.name||'匿名';   this.old = param.old||'保密';   this.address = param.address||'未知地区';   this.introduce = function(){    console.log('我叫'+this.name+',年龄'+this.old+',来自'+this.address+'。');   };  }    var xiaobai = new Cat({name:'小白',address:'火星'});  var xiaohei = new Cat({name:'小黑',old:'1岁'});  var xiaohua = new Cat({name:'小花',old:'3岁',address:'金星'});  var niming = new Cat({old:'1岁半'});  xiaobai.introduce();  xiaohei.introduce();  xiaohua.introduce();  niming.introduce();    //查看 数组prototype方法  console.log(Array.prototype);    //在数组原型链上添加构造函数  Array.prototype.myFun = function(){   console.log(this);   var retArr = [];   for(var i=0;i<this.length;i++){    if(this[i]%2 == 1){     retArr.push(this[i]);    }   }   return retArr;  };    Array.prototype.myJoin = function(str){   var str = str || '';   var retStr = '';   for(var i=0;i<this.length;i++){    if(i == this.length-1)    retStr += this[i];    else    retStr += this[i] + str;   }   return retStr;  };    var arr = [1,2,3,4,5,6,7,8,9];  console.log(arr.myFun());    console.log(arr.myJoin());  console.log(arr.myJoin('+')); </script></html>









原文转载:http://www.shaoqun.com/a/691741.html

跨境电商:https://www.ikjzd.com/

acedota:https://www.ikjzd.com/w/2070

跨境通网站:https://www.ikjzd.com/w/1329


<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title></head><body><h1style="text-align:center;">内容在控制台</h1><
c88是什么:https://www.ikjzd.com/w/1017.html
i号店:https://www.ikjzd.com/w/2263
sgshop:https://www.ikjzd.com/w/1982
如何通过亚马逊数据分析工具有效分析竞争对手? :https://www.ikjzd.com/home/19037
给力!中国邮政与华为结为全面战略合作伙伴!:https://www.ikjzd.com/home/97370
Shopify第三季度继续保持强劲增长 平台卖家超100万!:https://www.ikjzd.com/home/109669