4. 面接でよくある質問のまとめ(これについて)
<script> グローバルの this は window を意味します this.a = 20 // window オブジェクトに属性 a を掛けます、値は 20 です var test = { a: 40, init: function () { // これはオブジェクト内に表示されます関数では、 this の値は現在のオブジェクトを表します console.log(this.a) } } test.init() </script> 2 <script> // this.a = 20 // var test = { // a: 40, // init: function () { // console.log(this.a) // } // } // // fn はウィンドウのプロパティです // var fn = test.init // fn( ) ; // window.fn() </script> 3 <script> // this.a = 20 // var test = { // a: 40, // init: function () { // // go はウィンドウのプロパティではありません // function go(){ // console.log (this ) // Window // console.log(this.a) // 20 // } // // go 関数はテストによって呼び出されないため、go の this は test にできません // // // go () ; // } // } // test.init() </script> 4 <script> // this.a = 20 // var test = { // a: 40, // init: function () { / /関数 go() { // console.log(this.a) // init:function(){ // } // リターンゴー; // } // } // var s = test.init() // s(); // 20 </script> 5 <script> // 関数 test(a){ // this.a = a; // } // test.prototype.a = 20; // test.prototype.init = function(){ // console.log(this.a) // } // var s = new test(30); // // s.init(); // 30 // test.prototype.init(); // 20 </script> <script> // this.a = 20; // var test = { // a:40, // console.log(this.a) // } // } // ;(function(){ // var fn = test.init; // fn() // 20 // })() // TypeError: {(中間値) (中間値)} は関数ではありません </script> 6 <script> // var f = function(a){ // return a; // } // var f = (a)=>{ // aを返します。 // } // var f = a=>{ // aを返します。 // } // var f = a=>a; // 箭头関数数中没有 this </script> <script> // this.a = 20; // var test = { // a:40, // init:()=>{ // // アロー関数には this がありません // console.log(this.a) // } // } // test.init() // 20 </script> <script> // this.a = 20; // var test = { // a: 40, // init: function() { // console.log(this.a) // } // } // var s = test.init.bind(this) / /s(); </script> 7 <script> // var s = { // a:function(){ // console.log(1) // }, // b:function(){ // console.log(this) // 表示当前这个对象 // } // } // var s = { // a:function(){ // console.log(1) // }, // b(){ // console.log(this) // 現在のオブジェクトを示します // } // } // sb( ) </script> <script> // var s = { // a: function () { // console.log(1) // }, // b() { // console.log(2) // } // } // var f = sabind(this) // new f(); // var p = sbbind(this) // new p(); </script> 8 <script> // 機能 () { // this .test = 11; // var s = { // var f = sabind(this) // console.log(this) // } // } // var f = sabind(this) // // f() // // new f(); </script> <script> // this.test = 11; // var s = { // a: function () { // console.log(1+this.test) // } // } // var f = sabind(this) // new f(); </script> <script> // this.test = 11; // var s = { // a:()=>{ // console.log(1 + this.test) // } // } // new f(); </script> 9 <スクリプト> // 関数 C2(a){ // this.a = a; // } // C2.prototype.a = "ラオ"; // // console.log((new C2().a)) // 未定義 // console.log((new C2("xxx").a)) // xxx // 関数 C2(a){ / / a = 110; // } // C2.prototype.a = "ラオ"; // console.log((new C2().a)) // lao // function C2(a){ // var a = 110; // } // C2.prototype.a = "ラオ"; // console.log((new C2().a)) // lao // 関数 C2(a){ // // SyntaxError: 識別子 'a' はすでに宣言されています // } // C2.prototype.a = "ラオ"; // console.log((new C2().a)) // lao </script> 10 <script> // 関数 C2(a) { // this.a = a; // } // C2.prototype.a = "ラオ"; // console.log((new C2()).a) // 未定義 // console.log((new C2().a)) // 未定義 </script> 11 <script> // 関数 test(a ){ // this.a = a; // } // test.prototype.a = 20; // test.prototype.init = function(){ // console.warn(this.a) // } // var s = new test(); // s. //未定義</script> 12 <script> // 関数 C1(名前) { // if (名前) this.name = 名前; // } // 関数 C2(名前) { // this.name = 名前; // } // 関数 C3(名前) { // this.name = name || 'fe'; // } // C1.prototype.name = "aaa"; // C2.prototype.name = "bbb"; // C3.prototype.name = "ccc "; // // aaaundefinefe // console.log((新しい C1().name) + (新しい C2().name) + (新しい C3().name)); </script> 13 <script> var a; // undefinend と文字列の連結により文字列が取得されます var str = a + "hello" console.log("......."+str) 関数 A(名前){ this.name = name; console.log(this)<script> // 無名関数を f に割り当てます // var f = function(){} // g は関数名です 関数式の場合、関数名を指定できます // var f = function g(){console.log("haha...")} // f() // 関数式の関数名で関数を呼び出すことはできません // g() // g は定義されていません // 関数式の関数名は関数本体で使用できます // var f = function g(){ // console.log(g) // g は関数名です。関数本体で使用できます / // g(); // この関数は g() を通じて呼び出すことができますが、終了がない場合は無限ループが発生します // // g = 1; // 基本的な割り当てg へのデータ型の割り当ては効果がありません // // g = [ ]; // g への参照データ型の割り当ては効果がありません // // g = {}; // g = function(){ // console.log( "xixi...") // } // console.log(g ) // } // f() // 関数式の場合、関数名を付けることができますが、関数の外から関数名を使用して関数を呼び出すことはできません。関数名を使用できます。または関数呼び出し (無限ループ)、関数名を再割り当てすることはできません // var a = function abc(num) { // abc = num; // console.log(num) // 1 // console.log(abc) // abc は関数です // console.log(typeof abc) // 関数 // } // a(1) </script> 14 <script> // var a = function abc(num) { // abc = num; // return 1; // } // a(1) // console.log(abc())// ReferenceError: abc が定義されていません </script> <script> // これはグローバル平均ウィンドウで表示されます // this.a = 20 // 属性 a をウィンドウ オブジェクトに掛けます。値は 20 です。 <script> // var test = { // a: 40, // init: function () { // // this はオブジェクトの関数に表示され、this の値は現在のオブジェクトを表します。 // console.log(this. a) / / } // } // test.init() </script> 15 <script> // this.a = 20 // var test = { // a: 40, // init: function () { // console .log(this.a) // } // } // // fn はウィンドウのプロパティです // var fn = test.init // fn(); // window.fn() </script > 16 // this.a = 20 // var test = { // a: 40, // init: function () { // // go はウィンドウのプロパティではありません // function go(){ // console.log(this) // ウィンドウ // console.log( this.a) // 20 // } // // go 関数はテストによって呼び出されないため、go では this を test にすることはできません // // // go(); // } // } // test .init () </script> <script> // this.a = 20 // var test = { // a: 40, // init: function () { // function go() { // console.log (この .a) // } // リターンゴー; // } // } // var s = test.init() // s(); // 20 </script> <script> // 関数 test(a){ // this.a = a; // } // test.prototype.a = 20; // test.prototype.init = function(){ // console.log(this.a) // } // var s = new test(30); // // s.init(); // 30 // test.prototype.init(); // 20 </script> 17 <script> // this.a = 20; // var test = { // a:40, // init:function(){ // console.log(this.a) // } // } // ;(function(){ // var fn = test.init; // fn() // 20 // })() // TypeError: {(中間値)(中間値)} は関数ではありません </script> 18 <script> // var f = function(a){ // を返します; // } // var f = (a)=>{ // aを返します。 // } // var f = a=>{ // aを返します。 // } // var f = a=>a; // 箭头関数数中没有 this </script> 19 <script> // this.a = 20; // var test = { // a:40, // init:()=>{ // } // } // // アロー関数には this がありません // console.log(this.a) // test.init() // 20 </script> 20 <script> // this.a = 20; // var test = { // a: 40, // init: function() { // console.log(this.a) // } // } // var s = test.init.bind(this) / /s(); </script> 21 <script> // var s = { // a:function(){ // console.log(1) // }, // b:function(){ // console.log(this) // 表示当前这个对象 // } // } // var s = { // a:function(){ // console.log(1) // }, // b(){ // console.log(this) // 現在のオブジェクトを示します // } // } // sb() </script> 22 <script> // var s = { / / a: function () { // console. log(1) // }, // b() { // console. log(2) // } // } // var f = sabind(this) // new f(); // var p = sbbind(this) // new p(); </script> 23 <script> // this.test = 11; // console.log (これ) // var s = { // a: function () { // var f = sabind(this) // } // } // var f = sabind(this) // // f() // // new f(); </script> 24 <script> // this.test = 11; // var s = { // a: function () { // console.log(1+this.test) // } // } // var f = sabind(this) // new f(); </script> 25 <script> // this.test = 11; // var s = { // a:()=>{ // console.log(1 + this.test) // } // } // new f(); </script> 26 <スクリプト> // 関数 C2(a){ // this.a = a; // } // C2.prototype.a = "ラオ"; // // console.log((new C2().a)) // 未定義 // console.log((new C2("xxx").a)) // xxx // 関数 C2(a){ / / a = 110; // } // C2.prototype.a = "ラオ"; // console.log((new C2().a)) // lao // function C2(a){ // var a = 110; // } // C2.prototype.a = "ラオ"; // console.log((new C2().a)) // lao // 関数 C2(a){ // // SyntaxError: 識別子 'a' はすでに宣言されています // } // C2.prototype.a = "ラオ"; // console.log((new C2().a)) // lao </script> 27 <script> // function C2(a) { // this.a = a; // } // C2.prototype.a = "ラオ"; // console.log((new C2()).a) // 未定義 // console.log((new C2().a)) // 未定義 </script> 28 <script> // 関数 test(a ){ // this.a = a; // } // test.prototype.a = 20; // test.prototype.init = function(){ // console.warn(this.a) // } // var s = new test(); // s.init(); //未定義 </script> <script> // 関数 C1(名前) { // if (名前) this.name = 名前; // } // 関数 C2(名前) { // this.name = 名前; // } // 関数 C3(名前) { // this.name = name || 'fe'; // } // C1.prototype.name = "aaa"; // C2.prototype.name = "bbb"; // C3.prototype.name = "ccc "; // // aaaundefinefe // console.log((新しい C1().name) + (新しい C2().name) + (新しい C3().name)); </script> 29 <script> var a; // undefinend と文字列の連結により文字列が取得されます var str = a + "hello" console.log("......."+str) 関数 A(名前){ this.name = 名前; console.log(this) console.log(this.name) // 未定義 console.log(typeof this.name) // 未定義 } new A(); </script> console.log(this.name) // 未定義 console.log(typeof this.name) // 未定義 } new A(); </script>