admin管理员组文章数量:1279050
Could you tell me how to translate this into TypeScript
?
using SharpKit.JavaScript;
namespace MyNamespace
{
[JsType(JsMode.Prototype)]
public class JsEventArgs
{
}
public delegate void JsEventHandler<in T>(object sender, T args) where T : JsEventArgs;
public delegate void JsEventHandler(object sender, JsEventArgs args);
}
And usage in the other class:
public event JsEventHandler<LayerVisivilityEventArgs> Changed;
I tried that:
module JsEvent {
export class JsEventArgs {
}
public JsEventHandler: (sender: Object, args: any) => void;
public JsEventHandler: (sender: Object, args: JsEventArgs) => void;
}
Could you tell me how to translate this into TypeScript
?
using SharpKit.JavaScript;
namespace MyNamespace
{
[JsType(JsMode.Prototype)]
public class JsEventArgs
{
}
public delegate void JsEventHandler<in T>(object sender, T args) where T : JsEventArgs;
public delegate void JsEventHandler(object sender, JsEventArgs args);
}
And usage in the other class:
public event JsEventHandler<LayerVisivilityEventArgs> Changed;
I tried that:
module JsEvent {
export class JsEventArgs {
}
public JsEventHandler: (sender: Object, args: any) => void;
public JsEventHandler: (sender: Object, args: JsEventArgs) => void;
}
Share
Improve this question
edited Nov 30, 2012 at 14:03
Picrofo Software
5,5713 gold badges25 silver badges37 bronze badges
asked Nov 30, 2012 at 14:01
NickonNickon
10.2k13 gold badges67 silver badges127 bronze badges
10
- 1 I'm confused as to what you are trying to do. What does SharpKit have to do with TypeScript? – Jude Fisher Commented Nov 30, 2012 at 14:05
- I'm translating a C# app onto TypeScript. I want to create an event that will work as the one from C# code. – Nickon Commented Nov 30, 2012 at 14:06
- And what's wrong with the TypeScript you've tried above? – Jude Fisher Commented Nov 30, 2012 at 14:07
- 1 It's not because they are public - it's because you can't add properties to a module. – Jude Fisher Commented Nov 30, 2012 at 14:40
- 1 The question is not clear at all. That JsEventHandler and JsEventArgs and JsMode — how would anybody guess their purpose? You need to define clearly what are you trying to achieve rather than giving your raw source code out without any explanation. – Oleg Mihailik Commented Nov 30, 2012 at 16:09
3 Answers
Reset to default 7I wrote a little class which behaves like the .NET Delegate class (which is implicitly created for an event in .NET)
export interface IDelegate {
(sender: any, ...parameters: any[]): void;
}
export class DelegateBuilder {
private callees: IDelegate[];
constructor() {
this.callees = [];
}
invoke(sender: any, ...parameters: any[]) {
for (var i = 0; i < this.callees.length; i++) {
var callee = this.callees[i];
if (caller)
callee(sender, parameters);
}
}
contains(callee: IDelegate) : bool {
if (!callee)
return false;
return this.callees.indexOf(callee) >= 0;
}
add(callee: IDelegate): DelegateBuilder {
if (!callee)
return this;
if (!this.contains(callee))
this.callees.push(callee);
return this;
}
remove(callee: IDelegate): DelegateBuilder {
if (!callee)
return this;
var index = this.callees.indexOf(callee);
if (index >= 0)
this.callees.splice(index, 1);
return this;
}
toDelegate() : IDelegate {
return (sender: any, ...parameters: any[]) => this.invoke(sender, parameters);
}
}
I think you need to stop thinking in C#, and begin thinking in TypeScript.
This code:
module JsEvent {
export class JsEventArgs {
}
public JsEventHandler: (sender: Object, args: any) => void;
public JsEventHandler: (sender: Object, args: JsEventArgs) => void;
}
... tries to add two properties to a module, but properties are only permitted on a class (or interface).
TypeScript doesn't really use delegates - you can wire up an event (for example, in JQuery), like this:
$('selector').click((e:JQueryEventObject) => {
// Do something here.
});
... or, avoiding JQuery:
var div: HTMLDivElement = new HTMLDivElement();
div.onclick = (e: MouseEvent) => {
// Do something here.
};
... or, without an anonymous function:
div.onclick = doSomething;
function doSomething(e:MouseEvent):void{
// Do something here.
}
I think the closest pattern you'll get to a delegate is something like this (cribbed from http://forum.unity3d./threads/9679-Javascript-Delegates!):
module JSEvents {
class JSEventClass {
public delegate:(e:string) => any;
constructor() => {
this.delegate = this.doSomething;
this.delegate("Hello, Delegate World");
}
public doSomething(e: string): any {
console.log("Doing Something: " + e);
}
}
}
... but in a TypeScript / JavaScript world, I don't see what this pattern achieves over just calling doSomething()
directly.
I think this is a close translation of the code in question. However, this doesn't address event handling, which is already covered by another answer.
module MyNamespace
{
export class JsEventArgs
{
}
export interface JsEventHandler<T extends JsEventArgs> {
(sender: any, args: T): void;
}
export interface JsEventHandler {
(sender: any, args: JsEventArgs): void;
}
}
本文标签: javascriptTranslate C delegates onto TypeScriptStack Overflow
版权声明:本文标题:javascript - Translate C# delegates onto TypeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741280317a2369970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论