this

What is context in _.each(list, iterator, [context])?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 02:32:54
I am new to underscore.js. What is the purpose of [context] in _.each() ? How should it be used? The context parameter just sets the value of this in the iterator function. var someOtherArray = ["name","patrick","d","w"]; _.each([1, 2, 3], function(num) { // In here, "this" refers to the same Array as "someOtherArray" alert( this[num] ); // num is the value from the array being iterated // so this[num] gets the item at the "num" index of // someOtherArray. }, someOtherArray); Working Example: http://jsfiddle.net/a6Rx4/ It uses the number from each member of the Array being iterated to get the

C# When To Use “This” Keyword [duplicate]

*爱你&永不变心* 提交于 2019-11-27 02:32:53
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: When do you use the “this” keyword? Hello, I understand that the This keyword is used to refer to an instance of the class, however, suppose I have a class called Life , which defines two fields, the person ( their name ) and their partner( their name ): class Life { //Fields private string _person; private string _partner; //Properties public string Person { get { return _person; } set { _person = value; } }

How to bind onclick handlers to `this` properly on React

南楼画角 提交于 2019-11-27 02:18:55
Explanation to why this is not a duplicate: My code is already working, I have included as a comment. The question is why the this context change when I include it to click handler function. I'm attempting a calculator project in React. The goal is to attach onclick handlers to number buttons so the numbers are displayed on the calculator display area. If the handler is written directly to render method it is working, however, if I'm trying from the ComponentDidMount I get an error this.inputDigit is not a function . How do I bind this.inputDigit(digit) properly? import React from 'react';

How to reference the caller object (“this”) using attachEvent

浪尽此生 提交于 2019-11-27 01:52:14
Using the method .attachEvent() in IE, how can I reference the caller object (the element that triggered the event) with this ? In normal browsers, using .addEventListener , the var this points to the element, while in IE it points to the window object. I need it to work with the following code: var element = //the element, doesn't matter how it is obtained element.addAnEvent = function(name, funct){ if(element.addEventListener) // Works in NORMAL browsers... else if(element.attachEvent){ element.attachEvent("on"+name, funct); //where the value of "this" in funct should point to "element" } }

this value in JavaScript anonymous function

懵懂的女人 提交于 2019-11-27 01:49:20
Can anybody explain to me why A is true and B is false? I would have expected B to be true as well. function MyObject() { }; MyObject.prototype.test = function () { console.log("A", this instanceof MyObject); (function () { console.log("B", this instanceof MyObject); }()); } new MyObject().test(); this is special. It refers to the object that the function is being called on behalf of (most commonly via dot syntax). So, in the case of A , the function is being called on behalf of a new MyObject object. B is in a different function that isn't explicitly being called on behalf of any object, so

JS Object this.method() breaks via jQuery

人盡茶涼 提交于 2019-11-27 01:37:50
I'm sure there's a simple answer to this, but it's Friday afternoon and I'm tired. :( Not sure how to explain it, so I'll just go ahead and post example code... Here is a simple object: var Bob = { Stuff : '' , init : function() { this.Stuff = arguments[0] } , doSomething : function() { console.log( this.Stuff ); } } And here it is being used: $j = jQuery.noConflict(); $j(document).ready( init ); function init() { Bob.init('hello'); Bob.doSomething(); $j('#MyButton').click( Bob.doSomething ); } Everything works, except for the last line. When jQuery calls the doSomething method it is

Javascript: why “this” inside the private function refers to the global scope?

二次信任 提交于 2019-11-27 01:37:27
问题 Consider the following code: function A() {} A.prototype.go = function() { console.log(this); //A { go=function()} var f = function() { console.log(this); //Window }; f(); } var a = new A(); a.go(); Why does 'this' inside function 'f' refers to the global scope? Why it is not the scope of function 'A' ? 回答1: JavaScript has a different concept of what the special name this refers to than most other programming languages do. There are exactly five different ways in which the value of this can

How does Function.bind.bind(Function.call) uncurry?

蓝咒 提交于 2019-11-27 01:23:54
问题 We have this line in my code base: var uncurryThis = Function.bind.bind(Function.call); That I'm trying to work through. Presumably, it uncurries. How do I work this out? I guess it's a version of Function.bind whose own this is bound to Function.call . Doesn't help me enough. And I haven't found any uses, so I'm not even sure if you call it standalone or need to call it "as a method", only, you know, bind it first. 回答1: It passes the call function to the bind function, with the bind function

jQuery/JavaScript “this” pointer confusion

自古美人都是妖i 提交于 2019-11-27 00:52:52
The behavior of "this" when function bar is called is baffling me. See the code below. Is there any way to arrange for "this" to be a plain old js object instance when bar is called from a click handler, instead of being the html element? // a class with a method function foo() { this.bar(); // when called here, "this" is the foo instance var barf = this.bar; barf(); // when called here, "this" is the global object // when called from a click, "this" is the html element $("#thing").after($("<div>click me</div>").click(barf)); } foo.prototype.bar = function() { alert(this); } Aaron Qian Welcome

requestAnimationFrame with this keyword

我的未来我决定 提交于 2019-11-27 00:37:14
I'm using webkitRequestAnimationFrame but I'm having trouble using it inside of an object. If I pass the this keyword it will use window and I can't find a way for it to use the specified object instead. Example: Display.prototype.draw = function(){ this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height); //Animation stuff here. window.webkitRequestAnimationFrame(this.draw); }; I have also tried this but to no avail: Display.prototype.draw = function(){ this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height); //Animation stuff here. var draw = this.draw; window