开发者

Can C# allow to override the this keyword like in Javascript to make Jquery-like library possible?

开发者 https://www.devze.com 2023-03-14 01:05 出处:网络
From http://devlicio.us/blogs/sergio_perei开发者_C百科ra/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx

From http://devlicio.us/blogs/sergio_perei开发者_C百科ra/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx

When defining event handlers in jQuery, the library will take care of overriding the value of this and make sure it contains a reference to the element that was the source of the event.

How does jQuery override the value of this? Keep reading.

apply() and call()

Can C#/.NET give that same power ? If not how can one build a jquery-like library for C# not for javascript only ?


No, there is no way to change this in C# but that is not required for building fluent interfaces with concepts like method chaining (see the method chaining wiki page for an example in C#).


No, C# does not allow you to redefine language elements.

C# is a statically typed language whereas javascript is a dynamic language - just one difference that means you can't override language elements in C# but can in javascript.

Regardless, I don't quite see how you would write one library that would work for both javascript and C#.


No, you can't. this is a reserved keyword that is set automatically, and you really want it to point to the right place, ie, the current instance of the class.

Besides, what would exactly a 'jquery-like library for C#' be like? I don't think the concept makes much sense.

0

精彩评论

暂无评论...
验证码 换一张
取 消