24h購物| | PChome| 登入
2012-04-14 08:55:14| 人氣881| 回應0 | 上一篇 | 下一篇

[JavaScript] Variables in functions

推薦 0 收藏 0 轉貼0 訂閱站台

var greeting = "Ahoy";


var greeting = "Ahoy";
var greet = function() {
    console.log(greeting);
};
greet();

var greeting = "Ahoy";

var greet = function () {
  // Local greeting variable.
  var greeting = "Hello";
  console.log(greeting);
};

greet();


var greeting = "Ahoy";

var greet = function () {
  // Local greeting variable.
  var greeting = "Hello";
  console.log(greeting);
};

greet();
console.log(greeting);

var sayHelloTo = function (name) {
  console.log("Hello " + name);
};

sayHelloTo("Amjad");
sayHelloTo("Morris");

/* Start global variable `greeting` scope */
var greeting = "Ahoy";

var greet = function () {
  /* Start local variable `greeting` scope */
  var greeting = "Hello";
 
  /* Start local variable `myName` scope */
  var myName = prompt("Input your name: ");
 
  console.log(greeting + " " + myName);
 
  /* End local variables `myName` and `greeting` scope */
};

greet();

// This will produce a `ReferenceError` since we are out
// of the `myName` variable scope.
console.log(myName);

台長: Morris
人氣(881) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: [學習]JavaScript |
此分類下一篇:[JavaScript] Return
此分類上一篇:[JavaScript] Defining Functions

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文