zoukankan      html  css  js  c++  java
  • JavaScript基础知识十四(原型链this和原型扩展)

    在原型模式中,this常用的有两种情况:

    在类中this.xxx=xxx;this->当前类的实例

    某一个方法中的this->看执行的时候"."前面是谁this就是谁

    1)需要先确定this的指向(this是谁)

    2)把this替换成对应的代码

    3)按照原型链查找的机制,一步步的查找结果

    function Fn(){
              this.x =100;
              this.y =200;
              this.getY =function(){
                  console.log(this.y);
              };
          }
          Fn.prototype = {
              constructor:Fn,
              y:300,
              getX:function(){
                  console.log(this.x);
              },
              getY:function(){
                  console.log(this.y);
              }
          };
          var f = new Fn;
          f.getX(); // 100 ->  this.x 替换成f.x
          f.__proto__.getX();//-> this是f._proto_ -> f,_proto_.x->undefined
          Fn.prototype.getX();//->undefined
          f.getY();// 200;
          f.__proto__.getY();//300

    练习:模拟slice方法

    Array.prototype.mySlice=function(){};

    考虑的情况:

    slice(n,m);

    slice(n);

    slice()

    n和m是负数

    n<m

    n和m的值超过数组的长度

    n和m不是有效数字

    要求不能使用数组内置的方法,比如添加不能使用push而使用ary[ary.length-1]=xxx

    2.实现一个需求

    (5).plus(10).reduce(2)   5+10-2

    Number.prototype.plus=function(n){}

    Number.prototype.reduce=function(n){}

  • 相关阅读:
    Code Reading chap2
    Code Reading chap4
    Code Reading chap6
    常用的一些sql基础语句汇总
    20170322、Linux常用命令汇总
    在windows上部署使用Redis
    20170322、php基础语法
    20170822、在Linux上部署使用Redis
    Linux安装配置SVN服务器
    Linux安装配置MySQL
  • 原文地址:https://www.cnblogs.com/zzzzzzzsy/p/6695414.html
Copyright © 2011-2022 走看看