Tuesday 15 January 2013

javascript - jasmine: scope of this -


jasmine documentation () from:

this keyword

one The second way of sharing the variable between the first, each, this and after each, is done through this keyword each before each sample, after each / this, it is in the form of the same blank object, which is the next spec Is empty on the first spaces. Each / it / each after each.

I understand that JavaScript is surrounded by real-world scope. So I hope that it will already be in the context of the context for different references (depending on the function) / first / each after each.

For example,

  description ('device', function () {var everyThis = null; first each (function () (each = this;);); ('Check this', except the function () {.toEqual (each);}};  

Then this test should be passed.

Did Jasmine change the behavior of this or did I find anything wrong?

this reference Used when using , First All , etc.

Here is an example example - note that all the expectations listed below pass :

  (function () (description ("first without", function () {(function () {// it is not inside the first) Call this.dog to each = " Spot "; Warning (this.dog);}) (); It should not be used (" this dog "from the property of this dog, function () {hope (this.dog) .toBeUndefined (); // Because the Any object that is referenced in 'dog' do not belong '}); }); Describe ("Before each test", function () (each (function () (this.dog = "spot";}); ("now should work because we use" first, function () { Hopefully (this .dog) .to assessment ("spots");});});}) (); In general, your thinking about 'this' is defined within the scope of the function, But this implementation in Jasmine shows how you can provide a specific object to refer to it ? 'Keywords if you want JavaScript in this is done in the normal way  is using Function.prototype.apply ()  which is the first for you  this  reference The parameter allows to pass in an arbitrary object as the function. 


No comments:

Post a Comment