最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript public function - Stack Overflow

programmeradmin5浏览0评论

In TypeScript I have this simple code:

namespace Customer {
   function onOpen() {
   }
 }

It generates:

var Customer;
(function (Customer) {
    function onOpen() {
    }
})(Customer || (Customer = {}));

In order for Kendo to use onOpen the JS needs to look like this (Notice the onOpen). Is this possible?:

var Customer;
(function (Customer) {
    Customer.onOpen = function () {
    }
})(Customer || (Customer = {}));

In TypeScript I have this simple code:

namespace Customer {
   function onOpen() {
   }
 }

It generates:

var Customer;
(function (Customer) {
    function onOpen() {
    }
})(Customer || (Customer = {}));

In order for Kendo to use onOpen the JS needs to look like this (Notice the onOpen). Is this possible?:

var Customer;
(function (Customer) {
    Customer.onOpen = function () {
    }
})(Customer || (Customer = {}));
Share Improve this question asked Nov 2, 2017 at 16:19 Ian VinkIan Vink 68.9k107 gold badges353 silver badges567 bronze badges 2
  • Are you getting an error at runtime with KendoUI? If so, can you add that? It would surprise me if that is the case, as there isn't much difference in those declarations. You could perhaps set a property that is a function instead of a method, I can write an answer for that. But adding the console error would be helpful. – Sean Newell Commented Nov 2, 2017 at 16:21
  • Actually disregard my ment, @MinusFour got it right - your function is just not exposed / exported. – Sean Newell Commented Nov 2, 2017 at 16:27
Add a ment  | 

2 Answers 2

Reset to default 5

Add export:

namespace Customer {
   export function onOpen() {
   }
 }

Produces:

var Customer;
(function (Customer) {
    function onOpen() {
    }
    Customer.onOpen = onOpen;
})(Customer || (Customer = {}));

You need to export exposed properties, and since functions are first class citizens, you can write typescript code like this :

namespace Customer {
  export const onOpen = () => {}
}

Or instead of a lambda just with normal function

namespace Customer {
  export const onOpen = function() {}
}

Or this will work

namespace Customer {
  export function onOpen() { }
}

That will generate a property that points to a function instead of a function member - essentially the same thing really.

Javascript output:

var Customer;
(function (Customer) {
    Customer.onOpen = function () { };
})(Customer || (Customer = {}));
发布评论

评论列表(0)

  1. 暂无评论