this

'this' keyword, not clear

…衆ロ難τιáo~ 提交于 2019-11-27 04:55:32
I get confused about ' this ' keyword in the following codes, there are two 'this': var Foo = function(string){ this.name=string // 1st-this } Foo.prototype.get_name = function(){ return this.name // 2nd-this } var myFoo = new Foo('John') the_name=myFoo.get_name() ' the_name ' is equal to 'John', the prototype method get the name by return this.name . But can anyone explain to me the 1st- this and 2nd- this , what do they stand for? In Javascript, the value of this is dependent on the way you call the function . There are 5 ways to call a function in JS, and they all have effect on this : new

Javascript “this” reference for onclick event not working

北慕城南 提交于 2019-11-27 04:46:47
问题 I'm trying to call a function with the "onclick" event as so: <td id="A1" onclick="move()" class="white"></td> <td id="A2" onclick="move()" class="painted bp"></td> <td id="A3" onclick="move()" class="white"></td> In the function itself, i refer to "this": function move(e){ var myId = this.id; alert("myId"); } When I run the whole thing, the alert says 'undefined'. When I try alert(this) I get [object window]. I'm working with IE9, btw. Thanks 回答1: When calling a function from an event

ES6 arrow function lexical this in V8

北慕城南 提交于 2019-11-27 04:40:59
问题 I have the following ES6 code using a fat arrow function: var test = { firstname: 'David', fn: function() { return ['one', 'two', 'tree'].map(() => this.firstname) } } console.log(test.fn()) According to how arrow functions are supposed to work I'd expect this to be the test object. ES6Fiddle, Traceur and Firefox produce the expected output which is ["David", "David", "David"] . When enabling those features in Chrome using chrome://flags/#enable-javascript-harmony , however, I get [undefined,

Excessive use of `this` in C++ [duplicate]

吃可爱长大的小学妹 提交于 2019-11-27 03:54:06
This question already has an answer here: When should I make explicit use of the `this` pointer? 12 answers I'm dealing with a large code base that uses the following construct throughout class MyClass { public: void f(int x); private: int x; }; void MyClass::f(int x) { ' ' this->x = x; ' ' } Personally, I'd always used and hence prefer the form class MyClass { public: void f(int x); private: int _x; }; void MyClass::f(int x) { ' ' _x = x; ' ' } The reasons I prefer the latter are that it is more succinct (less code = fewer potential bugs), and that I don't like having multiple variables of

Difference between $(this) and this in jquery

微笑、不失礼 提交于 2019-11-27 03:47:10
What is the fundamental difference between using $(this) vs this $('.viewComments').click(function(ev){ //returns the desired value alert(this.getAttribute('id')); //Gives an error sayin function is not defined alert($(this).getAttribute('id')); //returns the desired value alert($(this).attr('id')); }); What I thought was "$(this)" will contain all functions that "this" has and more..But that doesn't seem to be the case. So what exactly is $(this)? and Hw do I know what functions are available when I'm using it? (I know I can get them through firebug. but I would like to know if there any some

'this' object can't be accessed in private JavaScript functions without a hack?

左心房为你撑大大i 提交于 2019-11-27 03:31:34
问题 I was working on a project for a while, trying to figure out what I was doing wrong, when I finally narrowed "the bug" down to the fact that the below code doesn't work as I expected: function Alpha() { this.onion = 'onion'; function Beta() { alert(this.onion); } Beta(); } alpha1 = new Alpha(); // Alerts 'undefined' However, if I change the code to this: function Alpha() { var self = this; this.onion = 'onion'; function Beta() { alert(self.onion); } Beta(); } alpha1 = new Alpha(); // Alerts

Android: why must use getBaseContext() instead of this

时间秒杀一切 提交于 2019-11-27 02:56:11
this often to reference to current context. But, at some case, why we must use getBaseContext() instead of this . (It means when use this will notice error). Here is my example: Spinner spinner = (Spinner) findViewById(R.id.spinner); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){ Toast.makeText(getBaseContext(),"SELECTED", Toast.LENGTH_SHORT).show(); //this line } At above code, when I change getBaseContext() to this will receive error. Who can explain for me

Javascript callbacks losing 'this'

五迷三道 提交于 2019-11-27 02:43:04
问题 I have an issuer where I lose the 'this' inside this 'object'. The output of the following piece of javascript gives me "some-id" and then "undefined". When I use 'this' inside a callback function, the scope goes out of the object and it cannot use 'this' anymore. How can I get the callback to use 'this' or at least have access to the object? Since I will make multiple objects, I won't be able to create a 'static' like storage. Please help this javascript n00b ;-) here is my test code that

this value is null in function (React-Native)

有些话、适合烂在心里 提交于 2019-11-27 02:40:37
问题 As per local testing, 'this' seems to be null inside the row render function. As a result this prevents me from binding a local function on the onPress prop. I have this render block: render() { return ( <ListView dataSource={this.state.dataSource} renderRow={this._renderRow} renderHeader={this._renderHeader} style={styles.listView} /> ); } and a local function _visitEntryDetail() { console.log('test'); } then row render _renderRow(something) { return ( <TouchableHighlight style={[styles

Why do I have to use $(this)? [duplicate]

与世无争的帅哥 提交于 2019-11-27 02:39:15
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: jQuery $(this) vs this In jquery sometimes I find that within a function I have to use $(this) because this won't work: var listItems = $('li'); listItems.each(function(index) { $(this).css({ }) }) Any ideas as to the reason why? 回答1: $() is the jQuery constructor function this is a reference to the DOM element of invocation So basically, you're turning a DOM reference into a jQuery object In your example, you