相关疑难解决方法(0)

JavaScript中的变量范围是什么?

javascript中的变量范围是什么?它们的内部是否与函数外部相同?或者甚至重要吗?另外,如果变量是全局定义的,那么它们存储在哪里?

javascript variables scope var function

1952
推荐指数
18
解决办法
48万
查看次数

node.js模块和函数中"this"的含义

我有一个加载的JavaScript文件require.

// loaded by require()

var a = this; // "this" is an empty object
this.anObject = {name:"An object"};

var aFunction = function() {
    var innerThis = this; // "this" is node global object
};

aFunction();

(function(anyParameter){
    console.log(anyParameter.anObject);
})(
    this // "this" is same having anObject. Not "global"
);
Run Code Online (Sandbox Code Playgroud)

我的问题是:this在var中a = this;是一个空对象,而this函数中的语句是node.js全局对象的阴影.我知道this关键字在函数中有所不同,但我无法理解为什么第一个this不等于全局,this而函数等于全局.

如何node.js的注入global,以this在功能范围,以及它为什么不其注入到模块的范围?

javascript node.js

35
推荐指数
3
解决办法
3万
查看次数

在Javascript中,为什么"this"运算符不一致?

在JavaScript中,"this"运算符可以在不同的场景下引用不同的东西.

通常在JavaScript"对象"内的方法中,它引用当前对象.

但是当用作回调时,它将成为对调用对象的引用.

我发现这会导致代码出现问题,因为如果你在JavaScript"对象"中使用一个方法作为回调函数,你无法判断"this"是指当前的"对象"还是"this"是指调用对象.

有人可以澄清如何解决这个问题的用法和最佳实践吗?

   function TestObject() {
            TestObject.prototype.firstMethod = function(){
                      this.callback();
                      YAHOO.util.Connect.asyncRequest(method, uri, callBack);

            }

            TestObject.prototype.callBack = function(o){
              // do something with "this"
              //when method is called directly, "this" resolves to the current object
              //when invoked by the asyncRequest callback, "this" is not the current object
              //what design patterns can make this consistent?
              this.secondMethod();
            }
            TestObject.prototype.secondMethod = function() {
             alert('test');
            }
        }
Run Code Online (Sandbox Code Playgroud)

javascript

26
推荐指数
2
解决办法
9208
查看次数

'this'关键字可以在RequireJS模块中使用吗?

我定义了一个RequireJS模块(见下文).它是站点上每个页面所需的函数集合.

返回对象中的一个函数initTwitter()需要在同一个对象中调用另一个函数shortenURL().我收到一个控制台错误,上面写着"TypeError:this.shortenURL不是函数".

所有这些函数最初都是在一个普通的Javascript单例对象中,代码运行正常.我是RequireJS的新手,所以我不确定这个关键字在RequireJS中的工作方式是否有所不同,或者我只是做错了什么.

编辑:我也尝试删除'this',但我得到一个"ReferenceError:shortenURL未定义"消息.

define(['config', 'jquery'], function(config, $) {
    /* Site-wide functions go here */

    return {
        doesObjPropertyExist: function(obj, prop) {
            var parts = prop.split('.');
                for(var i = 0, l = parts.length; i < l; i++) {
                    var part = parts[i];
                    if(obj !== null && typeof obj === "object" && part in obj) {
                        obj = obj[part];
                    }
                    else {
                        return false;
                    }
                }
            return true;
        },
        /** 
         * Shorten URL via bit.ly
         * Requires a callback function, so that …
Run Code Online (Sandbox Code Playgroud)

javascript requirejs

5
推荐指数
1
解决办法
1666
查看次数

嵌套函数和JavaScript中的"this"关键字

" this关键字始终引用包含函数的方法的对象."

很棒,听起来很简单,但这就是我想知道的......

例如:

function func1() {

  function func2() {
    alert(this == window); // true
  }

  func2();

  alert(this == window); // true
}

func1.func3 = function () {

  alert(this == window); // false
  alert(this == func1); // true

};

func1();
func1.func3();
Run Code Online (Sandbox Code Playgroud)

现在,因为func1实际上是global(window)对象(分配给全局对象的属性func1的函数对象)的方法,所以内部引用全局对象是有道理的,并且因为它是函数对象的方法,所以它是有意义的该内指的功能对象.thisfunc1func3func1thisfunc3func1

困扰我的是func2.我知道this嵌套函数内部也应该引用全局对象,但我不确定为什么因为func2它不是全局对象的方法.据我所知(这是我可能完全错误的部分)func2是一个func1调用(激活/变量)对象的方法.现在,如果我对此是对的(并且我不确定我是)那么不应该this在内部func2 …

javascript this

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

"这个"如何在javascript中运行?

嗨,我对thisjavascript的确切工作方式有点困惑.基于这个例子:

var myFunction = function(){
 function testMe(){
  console.log(this)  --------> DOMwindow
 }
 console.log(this) ---------> myFunction
}

var myvariable = new myFunction();
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

javascript

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

需要在函数对象上调用JavaScript方法

采用一个简单的匿名函数,它接受3个参数:

function hello(firstname, surname, city) {
    console.log('Hi ' + firstname + ' ' +
                        surname + '. I see you\'re from ' + city)
}
Run Code Online (Sandbox Code Playgroud)

使用函数方法"call"调用此函数有什么好处,只需调用函数?,即.

hello('Jane','Mansfield','Philadelphia');
Run Code Online (Sandbox Code Playgroud)

VS

hello.call(this,'Jane','Mansfield','Philadelphia');
Run Code Online (Sandbox Code Playgroud)

Fiddle-dee-dee:http://jsfiddle.net/wC3xz/1/

对不起,但是查看文档并没有任何消息.我唯一能想到的是你是否可以访问传递给函数的this对象.但是不会从匿名函数中访问这个在匿名函数即窗口的上下文中吗?

何时需要调用而不仅仅是functionname(args)?

javascript

3
推荐指数
1
解决办法
141
查看次数

标签 统计

javascript ×7

function ×1

node.js ×1

requirejs ×1

scope ×1

this ×1

var ×1

variables ×1