"This" in Function Parameter

this in function parameter

This is the syntax for declaring extension methods, a new feature of C# 3.0.

An extension method is part code, part compiler "magic", where the compiler with the help of intellisense in Visual Studio make it appear that your extension method is actually available as an instance method on the object in question.

Let me give an example.

There's no method on the String class that is named GobbleGobble, so let's create an extension method:

public static class StringExtensions
{
public static void GobbleGobble(this string s)
{
Console.Out.WriteLine("Gobble Gobble, " + s);
}
}

The class name is just my naming convention, it isn't necessary to name it like that, but it has to be static, as do the method.

After declaring the above method, you can, in Visual Studio, type this:

String s = "Turkey Baster!";
s.

after the dot, wait for intellisense, and notice there is a GobbleGobble method there, complete the code like this:

String s = "Turkey Baster!";
s.GobbleGobble();

Important: The class where the extension method is declared must be available to the compiler and the intellisense processor in order for intellisense to show the method. If you type in GobbleGobble manually, and use the Ctrl+. shortcut, it will not help you get the right using directives into the file.

Notice that the parameter to the method has disappeared. The compiler will silently move around the important bits, which are:

String s = "Turkey Baster!";
s.GobbleGobble();
^ ^
| +-- the compiler will find this in the StringExtensions class
|
+-- will be used as the first parameter to the method

Thus, the above code will be transformed by the compiler to this:

String s = "Turkey Baster!";
StringExtensions.GobbleGobble(s);

So at call-time, there's nothing magical about it, it's just a call to a static method.

Note that if your extension method declares more than one parameter, only the first supports the this modifier, and the rest has to be specified as part of the method call as normal:

public static void GobbleGobble(this string value, string extra)
{ | |
... | |
} | |
| |
+--------------------------------------------+ |
| |
v |
s.GobbleGobble("extra goes here"); |
^ |
| |
+-----------------------------------+

Extension methods was added in part due to Linq, where the Linq syntax of C# will look for appropriately named extension methods for the objects in play, which means you can "introduce" Linq-support into any type of class by just declaring the right extension methods. Of course, full Linq support is a lot of work, but it is possible.

Also, extension methods by themselves are really useful, so read up on it.

Here's a few links:

  • MSDN: Extension Methods (C# Programming Guide)
  • MSDN Magazine - Basic Instincts: Extension Methods
  • Wikipedia: Extension Method

Javascript 'this' as parameter

That closure, I do not think it means what you think it means.

This line here:

     fromField.onchange = function(){updateToField(toField,this)};

means "assign to onchange a function that assigns the contents of that fields to whatever toField is at the time of the change!

Since you only have one variable toField all the changeable fields will be assigned to it.

This would work:

var setOnChange = function(fromField, toField) {    
fromField.onchange = function(){updateToField(toField,this)};
};

for (var j = 0; j < inputs.length; j++)
{
if (inputs[j].name.indexOf('FROM') != -1 && if (inputs[j+1].name.indexOf('TO') != -1)
{
setOnChange(inputs[j], inputs[j+1]);
}
}

EDIT: Isaac might have a better explanation of the problem (although I don't really like his solution).

this keyword for function parameter

You can (since version 2.0 of typescript) specify what is the this you're expecting when a function is invoked.

As described in Specifying the type of this for functions:

Following up on specifying the type of this in a class or an
interface, functions and methods can now declare the type of this they
expect.

By default the type of this inside a function is any. Starting with
TypeScript 2.0, you can provide an explicit this parameter. this
parameters are fake parameters that come first in the parameter list
of a function

Notice that this won't get translated into js, so it's not a real argument in the function.

Can I pass this as a parameter to another function in javascript

The javascript functions call() and apply() are both for precisely for the purpose of calling a function within a context.

function sum() { 
return this.num1 + this.num2;
}

function callSum(num1, num2) {
this.num1 = num1
this.num2 = num2
return sum.call(this); //call sum() in the context of this
}

alert(callSum(10, 15));

function applySum(num1, num2) {
this.num1 = num1
this.num2 = num2
return sum.apply(this); //call sum() in the context of this
}

alert(applySum(30, 45));

jsfiddle example link

Now in the sum() function the this keyword had the same context as it does in the callSum() and applySum() functions.

The difference between call() and apply() is that apply's second parameter is either an array of parameters to pass or an arguments object.

jQuery pass $this to function parameter

Each time you call $, it returns a different result set object, even if the result contents are the same. The check you have to do is:

if (pauseMe.is("#leftBubble")) {

javascript function parameter with object notation

This is parameter destructuring, from ES2015. In the second case, you're initializing a local variable to the value of the items property of the argument.

function updateSomething({items}) {

is roughly equivalent to

function updateSomething(obj) {
var items = obj.items;

Some other examples here and here.

And from the MDN: Pulling fields from objects passed as function parameter

Note that this syntax isn't yet available in Edge or Safari (see compatibility map) so you might want to use a transpiler like Babel.

Is it necessary to add a argument to a function with parameters in JS

In JavaScript, functions are first-class objects. You can do anything to a function that you can do to an object. This includes passing it as the argument of a function.

addEventListener("click", respondToTheClick)

… passes respondToTheClick as the second argument of addEventListener.

addEventListener('click', respondToTheClick(event) );

is equivalent to:

let return_value = respondToTheClick(event);
addEventListener('click', return_value);

The second argument of addEventListener needs to be a function. The return value of respondToTheClick(event) is not a function. That is why you get an error.

When some other code, built into the browser, and inside the addEventListener function calls the function you pass to it as an argument, then that code will pass it an Event object as an argument.

function handleEvent(e) {
console.log(e);
}

function addEventDemo(event_type, event_handler) {
event_handler("E is " + event_type);
}

addEventDemo("click", handleEvent);

Pass a JavaScript function as parameter

You just need to remove the parenthesis:

addContact(entityId, refreshContactList);

This then passes the function without executing it first.

Here is an example:

function addContact(id, refreshCallback) {
refreshCallback();
// You can also pass arguments if you need to
// refreshCallback(id);
}

function refreshContactList() {
alert('Hello World');
}

addContact(1, refreshContactList);


Related Topics



Leave a reply



Submit