相关疑难解决方法(0)

Javascript中的名称解析?

我正在阅读这篇文章,我有一些问题请:

考虑这段代码:

1:   var a = 1;
2:   function b () {
3:       a = 10;
4:       return;
5:       function a() {}
6:   }
7:   b();
8:   alert(a);
Run Code Online (Sandbox Code Playgroud)

这将提醒1.(我的问题是为什么?)

文章指出它与名称解析有关.

名称解析(根据文章)由此订单决定:

1. Internal mechanisms of language: for example, in all scopes are available “this” and “arguments”.
2. Formal parameters: the functions can be named as the formal parameters, which scope is limited to the function body.
3. Function declarations: declared in the form of function foo() …
Run Code Online (Sandbox Code Playgroud)

javascript hoisting

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

AngularJS功能提升

我有一个'初学者'的问题.为什么会出错?我在代码中调用该函数,但该函数被进一步定义.

AngularJS版本:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.name = 'Someone';
    $scope.doStuff(); // run the defined function, but errors out

    $scope.doStuff= function(){  // function definition
        console.log('did something');
    }
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/2fjuJ/4/

但这个工作正常:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.name = 'Someone';
    $scope.doStuff = function(){
        console.log('did something');
    }

    $scope.doStuff(); 
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/2fjuJ/5/

javascript angularjs

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

JavaScript范围和提升:代码按预期返回1而不是10

我是JavaScript的新手,我真的不太明白为什么下面的代码返回1而不是10:

var a = 1;
function b() {
    a = 10;
    return;
    function a() {}
}
b();
alert(a);
Run Code Online (Sandbox Code Playgroud)

运行代码:http://jsfiddle.net/smMtU/

如果我撤消该行function a() {},它将10按预期返回.这段代码来自这篇文章,解释了JavaScript中的概念范围和提升.也许我在阅读这篇文章时遗漏了一些东西?

请有人指出这段代码背后的概念吗?

javascript

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

函数调用javascript

我打电话给helloworld并用以下两种不同的方式定义它:

1)随变量

2)使用函数名称itseld

var helloWorld = function() {
    return '2';
}

function helloWorld() {
    return '1';
}

alert (helloWorld());  // This alert 2, but in absence of "var helloWorld = ....", it alert "1".
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么它调用var helloWord =?而不是函数helloWorld()?

谢谢 !!

javascript alert function

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

为什么此代码的输出是242,而不是243

var x = 2;

function fun() {
  x = 3;
  var x = 4;
  document.write(x);
}

document.write(x);

fun()

document.write(x);
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我了解控制流程。为什么输出242在看起来应该是243时会如此。将不胜感激所有帮助。

html javascript

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

JS中的变量提升

我在这里对可变提升概念有点困惑。为什么第一个console.log(flag)输出undefined?它不应该捕获已经初始化的 false 值在作用域链中向上移动吗?

var flag = false;
(function(){
    console.log(flag);
    var flag = true; // JavaScript only hoists declarations, not initialisations

    console.log(flag);

    if(flag){
        let name = "John";
        const age = "24";

        console.log(name);
        console.log(age);
    }

    //console.log(name); //ReferenceError: name is not defined ( as name is block scoped here )
    //console.log(age);  //ReferenceError: age is not defined ( as age is block scoped )

})();
Run Code Online (Sandbox Code Playgroud)

javascript

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

为什么这个全局变量在函数内未定义?

如果在同一个函数中重新声明和定义了相同的全局变量,为什么这个全局变量在函数中未定义?

var a = 1;
function testscope(){
 console.log(a, 'inside func');
 //var a=2;
};
testscope();
console.log(a, 'outside func');

output:
1 "inside func"
1 "outside func" 
Run Code Online (Sandbox Code Playgroud)

考虑相同的代码,其中 var a = 2; 内部功能块未注释

var a = 1;
function testscope(){
 console.log(a, 'inside func');
 var a=2; 
};
testscope();
console.log(a, 'outside func');

Output
undefined "inside func"
1 "outside func"
Run Code Online (Sandbox Code Playgroud)

javascript

0
推荐指数
1
解决办法
1775
查看次数

为什么`a`在下面的函数中未定义?

 a = 10;

function abc() {
 console.log(" a is " + a);
var  a =5;
console.log("after a is " + a);
}

abc();
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我的第一个console.log显示a为undefined,而第二个console.log显示a为5 的结果.

我仍然试图理解为什么我的第一个控制台显示a为未定义.我已经将变量定义a为全局变量.

javascript

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

javascript变量的怪异行为

情况1 - 如果我在变量声明之前调用console.log(变量),我将得到未定义.例如;

  // code 
  console.log(a);
  var a ;

  // output
  undefined
Run Code Online (Sandbox Code Playgroud)

情况2 - 如果我没有变量声明的console.log(变量),我得到Uncaught ReferenceError:未定义变量.

// code
console.log(a);

// output
Uncaught ReferenceError: a is not defined
Run Code Online (Sandbox Code Playgroud)

但是我们可以在函数定义之前或之后调用函数,但它从不会产生任何问题.例如;

  console.log(example());

  function example(){
    return 'test done';
  }
  console.log(example());

 // output without any issue
Run Code Online (Sandbox Code Playgroud)

现在的问题是,是什么样的区别不确定的没有定义.

javascript

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

标签 统计

javascript ×9

alert ×1

angularjs ×1

function ×1

hoisting ×1

html ×1