对象

作者: Luyc_Han | 来源:发表于2017-11-01 13:09 被阅读7次
var temp =  0
Array.temp = 1
这两者的区别在于第一个叫变量,第二个叫做属性.需要辨别
  • prototype
/// 给一个集合添加属性
  <script type="text/javascript">
    window.onload =  function () {
      Array.prototype.sum = function () {
        let add = 0
        this.map(function (obj) {
          add = add + obj
        })
        return add
      }
      let arra = [1,2,3,4,5,6,7]
      alert(arra.sum())
    }
  </script>
  • this
指的是当前所在作用域的对象
比如:
  <script type="text/javascript">
    function show() {
      alert(this)
    }
    show()
  </script>

此时的this就是当前的window说白了show()也可以这么写window.show()此时show就是window的属性

相关文章

网友评论

      本文标题:对象

      本文链接:https://www.haomeiwen.com/subject/ksywpxtx.html