2016-12-17 12 views
-1

私は私がそれを実行するとき、私はこのコードを持っています未定義表示されます。ただし、このキーワードでグローバルプロパティにアクセスできます。なぜ私は未定義の値を得ています

var firstName = "Peter", 
     lastName = "Ally"; 

     function showFullName() { 
     // "this" inside this function will have the value of the window object 
     // because the showFullName() function is defined in the global scope, just like the firstName and lastName 
     alert (this.firstName + " " + this.lastName); 
     } 
     showFullName(); 
+1

http://jsbin.comを生産するとのことに注意してください。/gerexi/1/edit?js、output - 私は問題を再現できません。 – Quentin

+0

私はそれほどOKではない –

+1

厳密なモードでは、それはちょうどエラーを投げる。これが_inside_別の関数に配置されていると、 "undefined undefined"と表示されます。 – vlaz

答えて

-1

通常、代わりにwindowキーワードを使用します。 thisはfuncitonが宣言された場所では独立していましたが、どこに呼び出されたか(そしてどのように)呼び出されたかに依存しています。

var firstName = "Peter", 
    lastName = "Ally"; 

function showFullName() { 
    alert (window.firstName + " " + window.lastName); 
} 
showFullName(); 
-1

私が厳密モードを使用すると、このキーワードはグローバル関数では未定義の値を保持することがわかりました。 strictモードで が、しかし、これの値はどんな時にそれが実行コンテキストに入るときに設定したままなので、次のような場合には、これは未定義にデフォルト設定されます:

function f2(){ 
     "use strict"; // see strict mode 
     return this; 
    } 

    f2() === undefined; 

ので、strictモードでは、これが実行コンテキストによって定義されていない場合、未定義のままです。このコードスニペットをMDNから取得しました。

私の場合、値はfiddleで表示されません。しかし、それは@vlazによって指摘される理由です。 - これは機能範囲でを配置した場合、それはしかし、動作しません適切に(簡単に例のためconsole.logalertを置き換え)

var firstName = "Peter", 
 
    lastName = "Ally"; 
 

 
function showFullName() { 
 
    // "this" inside this function will have the value of the window object 
 
    console.log("this and window are the same thing", this === window); 
 
    
 
    // because the showFullName() function is defined in the global scope, just like the firstName and lastName 
 
    console.log(this.firstName + " " + this.lastName); 
 
} 
 

 
showFullName();

を実行した場合

+0

上記のコードより厳密なモードが有効になっていると、エラーが発生します。 – vlaz

1

@vlazこの作品のおかげで恐らくJSフィドルはそのようなことをします

(function() { 
 
    var firstName = "Peter", 
 
     lastName = "Ally"; 
 

 
    function showFullName() { 
 
     // "this" inside this function will still have the value of the window object 
 
     console.log("this and window are the same thing", this === window); 
 
     
 
     // however firstName and lastName are not goint to be attached to it because they are in functional scope 
 
     console.log("the names are still reachable", firstName, lastName) 
 
     
 
     //but not attached to the window object (which "this" points to) 
 
     console.log(this.firstName + " " + this.lastName); 
 
    } 
 

 
    showFullName(); 
 
})();

あなたはstrict mode enabledを持っているならば、thisundefined代わりのwindowになり、コードがエラー

var firstName = "Peter", 
 
    lastName = "Ally"; 
 

 
function showFullName() { 
 
    "use strict"; 
 
    // "this" inside this function will now have the value "undefined" 
 
    console.log("'this' is actually 'undefined'", this); 
 
    console.log("the actual value 'undefined', not a string", typeof this); 
 
    
 
    // the following line will throw a TypeError because it's trying to get a property from "undefined" 
 
    console.log(this.firstName + " " + this.lastName); 
 
} 
 

 
showFullName();

関連する問題