zoukankan      html  css  js  c++  java
  • codewars 知识梳理(一)

    刷题时,总用不会的,开此贴总结;

    • 实现加减乘除的链式操作。举个栗子,(3).add(5).multiply(2)   这样的输出16。  

    这题我好像并不会,原因还是没能理解this这个玩意,不懂怎么用链把他们接起来,先看看答案吧。

    Number.prototype.add      = function(n){ return this+n }
    Number.prototype.subtract = function(n){ return this-n }
    Number.prototype.multiply = function(n){ return this*n }
    Number.prototype.divide   = function(n){ return this/n }
    Number.prototype.square   = function(){ return this*this }

    答案就是这么简洁,好像很牛逼的样子。

    然而 这里的this  不好理解 

    查了查关于this的资料:一般而言,在Javascript中,this指向函数执行时的当前对象。

    还有一句  this该关键字在Javascript中和执行环境,而非声明环境有关。

    var someone = {
        name: "Bob",
        showName: function(){
            alert(this.name);
        }
    };
    
    var other = {
        name: "Tom",
        showName: someone.showName
    }
    
    other.showName();  //Tom

    this关键字虽然是在someone.showName中声明的,但运行的时候是other.showName,所以this指向other.showName函数的当前对象,即other,故最后alert出来的是other.name。

    这篇挺好的  更多this  移步这里 http://www.cnblogs.com/justany/archive/2012/11/01/the_keyword_this_in_javascript.html

    然后呢  理解理解就想通了。

  • 相关阅读:
    zip
    yield
    日记
    cat
    genfromtext
    pytorch易忘
    小程序入门(一)
    (二)连接数据库使用Model对象对数据库进行操作(CRUD)
    (一)Moogose(node.js对数据库进行操作)
    CURD练习
  • 原文地址:https://www.cnblogs.com/liuestc/p/4480729.html
Copyright © 2011-2022 走看看