AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 17.04.2015, 08:00   #1  
Blog bot is offline
Blog bot
Участник
 
25,459 / 846 (79) +++++++
Регистрация: 28.10.2006
Switching from C# to JavaScript to develop your control add-ins might get you scratching your head more often that your scalp, or nails for that matter, might be happy with.

One of those is overloading. In C#, this is a no-brainer:



However, when you want to do that in JavaScript, if you are not a JavaScript developer, making this work is not as straightforward as a regular JavaScript Joe would find it.



So, consider this C# interface for the JavaScript control add-in for a second:



This, for sure, won’t get the job done:



The reason why not is that all the calls below:



… will result in exactly the same result:



The problem (well, not really a problem) with JavaScript is you can’t overload a function. Just like C/AL, you can only have one function with any given name – it’s the name that defines the function, not its signature. However, JavaScript runtime won’t complain if you declare three functions with the same name – it will simply overwrite the existing function of the same name. It’s not a bug, or a misbehavior, or something of the sort. On the contrary, it’s how it’s designed.

In JavaScript functions are really variables. So, as much as you are allowed to do this:



… you are also allowed to do the same with the functions. My code above with three functions is in not really way too different than this:



And this explains just why no matter with which parameters I call the HelloWorld function, it is always calling the last function assigned to that name. The others are gone, out of scope.

Being a C/AL guy/gal and all, consider it for a moment – you’ll find this feature beautiful – I promise.

There is an even nicer feature of JavaScript that you’ll quickly start to love – in JavaScript you don’t have to really match the types and numbers of arguments when calling functions. No matter how many parameters the function declares, you can call it with an arbitrary number of arguments.

And here lies the key to overloading. C# needs to overload the function in the interface definition, because this allows C/AL to call the (same) function with different arguments, but JavaScript only ever needs one definition alone. Something like this (not to say that this is necessarily the only, or even the best way):



Then, your C/AL code can call any of the three interface overloads, and it will always be the same JavaScript function responding. Then, from inside the function we check the number (and/or types) of arguments to decide what exactly to do or return.

And that’s it. Simple, and elegant.

Read this post at its original location at http://vjeko.com/blog/overloading-me...ontrol-add-ins, or visit the original blog at http://vjeko.com. 5e33c5f6cb90c441bd1f23d5b9eeca34The post Overloading Methods With JavaScript Control Add-ins appeared first on Vjeko.com.

</img> </img>

Читать дальше
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 15:22.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.