相关疑难解决方法(0)

JavaScript闭包如何工作?

您如何向知道其所包含概念的人(例如函数,变量等)解释JavaScript闭包,但不了解闭包本身?

我已经看过维基百科上给出的Scheme示例,但遗憾的是它并没有帮助.

javascript variables closures scope function

7644
推荐指数
82
解决办法
139万
查看次数

"this"关键字如何运作?

我注意到,似乎没有明确解释this关键字是什么以及如何在Stack Overflow站点上的JavaScript中正确(和错误地)使用它.

我亲眼目睹了一些非常奇怪的行为,并且无法理解为什么会发生这种行为.

this工作如何以及何时使用?

javascript this

1243
推荐指数
16
解决办法
39万
查看次数

需要帮助理解JavaScript对象

我试图在javascript中理解对象.这是代码:

var fn={};
var canvas;
var ctx;
fn.game=function(width,height,inSide,name){
    this.canvas2=document.getElementById(inSide).innerHTML = "<canvas id="+name+" style='width:"+width+";height:"+height+";'>Your browser does not support the Canvas Element.</canvas>";
    this.canvas=document.getElementById(name);
    this.ctx=this.canvas.getContext("2d");
    document.getElementById(inSide).style.width=width;
    document.getElementById(inSide).style.height=height;
    canvas=document.getElementById(name);
    ctx=this.canvas.getContext("2d");
    this.width=width;
    this.height=height;
    canvas.width=width;
    canvas.height=height;
    this.add={

    };
    this.add.state=function(name){
        this[name]=3;
    };
};


var game=new fn.game(640,480,"game","canvas");

game.addState("play");
Run Code Online (Sandbox Code Playgroud)

当我引用这个["name"]时,我试图将它引用到fn.game,但是这个dous不起作用,因为它引用了最本地的对象.关于如何做到这一点的任何想法?

javascript object this

4
推荐指数
1
解决办法
94
查看次数

这在JavaScript函数中引用了什么?

function Box(width, height)
{
  this.width = width;
  this.height = height;
}

var myBox = new Box(5,5);
Run Code Online (Sandbox Code Playgroud)
  1. 什么是new关键词技术上在这里做什么?它是否正在创建一个新功能?或者是创建一个新对象并将该函数应用于它?

  2. 如果是这样,那么这是一种创建"Box"的方法,这是否意味着this关键字实际上是指对象myBox?

javascript prototype function

2
推荐指数
1
解决办法
1402
查看次数

标签 统计

javascript ×4

function ×2

this ×2

closures ×1

object ×1

prototype ×1

scope ×1

variables ×1