(質問を4回修正しました.custLogの変更部分は対応する変更番号でコメントアウトされています。前のバージョン - この説明の最後にあります)Loggin関数のスコープとこれ。 TypeError:custLog(...)。bindは関数ではありません
窓、内外の関数にthis
の値を表示しようとしました。
<script type="text/javascript">
//HELPING function
function whatTypeFnc(obj) {
// Angus Croll https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase(); //array, object, function
};
//The last changes, the custLog up to date.
//CustLog is helping function to log if the variables in array names are in the scope of the calling function, and to log their values.
var custLog = function (num) {
var nameF = this;
var names = ['outerVal', 'later', 'innerVal' , 'innerF', 'paramVal', 'tooLate', 'outerF'];
var ans = num + ' ' + nameF + " : "; //nameF = : ,
if (this) { names.forEach(function(item, index, array) {
try {
var nameVar = eval(item);
if(eval(nameVar)) {
if (whatTypeFnc(nameVar) !== 'function') {
ans+= item + ' = ' + nameVar + ', ';
} else { ans+= item + ' = function, '; } } }
catch (e) { ans+= item + ' = und, '; }
}); }
console.log(ans);
return ans;
};
// the code where i use custLog() to log information about the application specific scope of inner, outer functions and window.
var outerVal = 'outerVal';
var later;
var that = this; //refers to window
function outerF() {
var that = this;
//custLog('43').bind(this); //gives the same error: TypeError: custLog(...).bind is not a function.
custLog('43').bind(that);
var innerVal = 'innerVal';
function innerF(paramVal) {
var that = this;
console.log('innerF this='+this);
custLog('47').bind(that);
assert(outerVal, "Inner can see the outerVal.");
assert(innerVal, "Inner can see the innerVal.");
assert(paramVal, "Inner can see the later_second.");
assert(tooLate, "Inner can see the tooLate,");
}
custLog('53').bind(that);
later = innerF;
};
custLog('56').bind(that); //gives the error that custLog('56').bind is not a function
assert(!tooLate,"Outer can't see the tooLate");
var tooLate = 'tooLate';
custLog('59').bind(that);
outerF();
custLog('61').bind(that);
later('later_second');
custLog('63').bind(that);
I am getting the TypeError: `custLog(...).bind is not a function`. Custlog is the following:
56 [object Window] : outerVal = outerVal, innerVal = und, innerF = und, paramVal = und, outerF = function,
TypeError: custLog(...).bind is not a function
error source line: custLog('56').bind(that);
// ***************************************** ****** 質問の前のコード。問題は、this
が関数変数を参照していないことでした。それは以下のコメントによって答えられましたが、今他のエラーが発生しました。実際には全く機能に戻って参照していない
this
:あなたの最初の質問に答えるために
I try to print value of `this` in window, inner and outer functions. My logs does not coincide with assert log. Why?
On line 47 `this` refers to `window` instead of `innerF`, one lines 43 and 53 - to `window` instead of `outerF`. Why?
Why i do not got logged `innerF` on line 47?
// Helping function for assertion and type checking are the same .
function assert(value, desc) { .. };
functio whatTypeFnc(obj) { ... };
var outerVal = 'outerVal';
var later;
var that = this; //refers to window
function outerF() {
var that = this;
console.log('innerF this='+this);
//custLog('43').bind(this); //gives the same error: TypeError: custLog(...).bind is not a function.
custLog('43').bind(that);
var innerVal = 'innerVal';
function innerF(paramVal) {
var that = this;
console.log('innerF this='+this);
custLog('47').bind(that);
assert(outerVal, "Inner can see the outerVal.");
assert(innerVal, "Inner can see the innerVal.");
assert(paramVal, "Inner can see the later_second.");
assert(tooLate, "Inner can see the tooLate,");
}
custLog('53').bind(that);
later = innerF;
};
custLog(that, '56');
assert(!tooLate,"Outer can't see the tooLate");
var tooLate = 'tooLate';
custLog(that, '59');
outerF();
custLog(that, '61');
later('later_second');
custLog(that, '63');
//The previous question custLog
var custLog = function (ArrObj, num) {
var nameF = this;
if (ArrObj) { nameF = ArrObj.toString(); } //does not work
else { nameF=ArrObj; }
var names = ['outerVal', 'later', 'innerVal' , 'innerF', 'paramVal', 'tooLate', 'outerF'];
var ans = num + ' ' + nameF + " : "; //nameF = : ,
if (ArrObj) { names.forEach(function(item, index, array) {
try { var nameVar = eval(item);
if(eval(nameVar)) {
if (whatTypeFnc(nameVar) !== 'function') {
ans+= item + ' = ' + nameVar + ', ';
} else { ans+= item + ' = function, '; } } }
catch (e) { ans+= item + ' = und, '; }
}); }
/* var descr = Object.getOwnPropertyDescriptor(ArrObj, item);
if(descr) {
if(ArrObj.hasOwnProperty(item)) {
if (whatTypeFnc(ArrObj[item]) !== 'function') {
ans+= item + ' = ' + descr.value + ', ';
} else { ans+= item + ' = function, '; } } } */
console.log(ans);
return ans;
};
// Helping function for assertion.
//From Secrets of JavaScript Ninja by John Resig, Bear Bibeault
function assert(value, desc) {
var resultsList = document.getElementById("results");
if (!resultsList) {
resultsList = document.createElement('ul');
document.getElementsByTagName('body')[0].appendChild(resultsList);
resultsList.setAttribute('id','results');
}
var li = document.createElement("li");
li.className = value ? "pass" : "fail";
li.appendChild(document.createTextNode(desc));
resultsList.appendChild(li);
}
The assert results are :
Outer can't see the tooLate
Inner can see the outerVal.
Inner can see the innerVal.
Inner can see the later_second.
Inner can see the tooLate,
The log results are :
56 [object Window] : outerVal = outerVal, later = undefined, tooLate = undefined, outerF = function,
59 [object Window] : outerVal = outerVal, later = undefined, tooLate = tooLate, outerF = function,
43 [object Window] : outerVal = outerVal, later = undefined, tooLate = tooLate, outerF = function,
53 [object Window] : outerVal = outerVal, later = undefined, tooLate = tooLate, outerF = function,
61 [object Window] : outerVal = outerVal, later = function, tooLate = tooLate, outerF = function,
47 [object Window] : outerVal = outerVal, later = function, tooLate = tooLate, outerF = function,
63 [object Window] : outerVal = outerVal, later = function, tooLate = tooLate, outerF = function,
Why `this` refer to `[object Window]` instead of `innerF` on line 47, or `outerF` on lines 43 and 53?
Why on line 47, my log does not see `innverVal=innerVAL` and `paramVal=later_Second`?
あなたが関数を呼び出すときthis' 'の値を設定する必要がある場合は、[Function.prototype.call](https://developer.mozilla.org/en-US/docs/Web/JavaScriptを参照してください/ Reference/Global_Objects/Function/call)と[Function.prototype.apply](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) – csum
ですそこから電話しています。 –