[英]Optional parameters in javascript function
I have a common function:我有一个共同的功能:
function getUrl() { return Xrm.Page.context.getClientUrl() + "/api/data/v9.0/"; }
But sometimes I need to use parent frame to get url, like this:但有时我需要使用父框架来获取 url,如下所示:
function getUrl() { return window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v9.0/"; }
Can I declare this common function with possibility to pass window.parent.
我可以通过
window.parent.
来声明这个通用函数吗window.parent.
if I need it?如果我需要它? And if the parameter is not passed, use the common implementation.
如果没有传递参数,则使用通用实现。
Just use a param:只需使用一个参数:
function getUrl(_use_parent){
let url;
if(_use_parent)
url = window.parent.Xrm.Page.context.getClientUrl();
else
url = Xrm.Page.context.getClientUrl();
return url + "/api/data/v9.0/";
}
If you just call getUrl(), _use_parent is undefined so it use the else statement.如果您只是调用 getUrl(),则 _use_parent 未定义,因此它使用 else 语句。
[edit] And if you really want to pass window.parent [编辑] 如果你真的想通过 window.parent
function getUrl(_window_parent){
let url;
if(_window_parent)
url = _window_parent.Xrm.Page.context.getClientUrl();
else
url = Xrm.Page.context.getClientUrl();
return url + "/api/data/v9.0/";
}
But if _window_parent isn't the correct object you could get a JavaScript error.但是如果 _window_parent 不是正确的对象,您可能会收到 JavaScript 错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.