[Momo gold] JS call this anonymous function variable error

background

Today, the use of Dcloud scan code function, success successful method to set the data in scanContext variables newspaper "undefined"!

Thinking

First, the scan code content is not suspected JSON object attributes can not point error. Exclusion, the use of JSON.parse conversion is successful, still reported "undefined".
Second, how to "undefined" it, baffled. Continue to find ways

solve

Suddenly, I see attachment method chosen, people are this.fileUrl set of success. Under comparison, what is the difference. Anonymous method defined discovery, different wording. I have a function (res), people are (res) => arrow. Try to go test,

Success! ! !

the reason

It is this JS anonymous method of pointing a problem, function declarations ways, this is pointed to itself, the parent of the variables are inaccessible. Please read the following source code, run reports an error it?

<script type="text/javascript">
var num = 100;
var thah={
success:function(){
alert(this.num);
}
}
thah.success();
</script>

Spread

Please read familiar friend with me: var that = this; // Dafa is good
sleeker arrow is declared success: (res) => { } more stylish, more popular on tall.

Above, it is the question of JS point of this anonymous function. Stepped pit, cheering together.

Guess you like

Origin blog.51cto.com/zl0828/2402896