简体   繁体   English

如何在JavaScript的一行中无错误地调用可能不存在的方法?

[英]How can I call a possibly-non-existent method, without errors, in one line in JavaScript?

I was writing a program that uses javascript and creates inheritance in a way. 我正在编写一个使用javascript并以某种方式创建继承的程序。

If the user types in: 如果用户键入:

x.isSingleLine()

and it does not exist, it crashes Javascript. 并且它不存在,它使Javascript崩溃。 I was hoping there was a way to do something like: 我希望有一种方法可以执行以下操作:

x.isSingleLine() || "Doesnt Exist";

to in a way have it do a soft error or some sort of alert. 以某种方式使它执行软错误或某种警报。 I didnt want to have to encapsulate the object in a try-catch block. 我不想将对象封装在try-catch块中。

Is there a way this can be accomplished? 有没有办法可以做到这一点?

In the statement: x.isSingleLine() || 在语句中: x.isSingleLine()|| "Doesnt Exist"; “不存在”; it will crash and not do the OR part, so this is not the answer apparently. 它将崩溃,并且不执行“或”部分,因此显然这不是答案。

How about that? 那个怎么样?

  x.isSingleLine ? x.isSingleLine() : console.log("Doesnt Exist");

Edit: As suggested by @FelixKling if there's a chance that isSingleLine exists but is not a function you should instead use: 编辑:如@FelixKling所建议,如果isSingleLine存在但不是函数,则应改用:

(x.isSingleLine && x.isSingleLine instanceof Function) ? x.isSingleLine() : console.log("Doesnt Exist");

您需要调用之前检查它是否存在。

var result = x.isSingleLine ? x.isSingleLine() : "Doesn't Exist";

As far as I know from the top of my head, you can use a try-catch block for that: 据我了解到的,您可以使用try-catch块:

try {
    x.isSingleLine();
} catch (e) {
    // Something happened
    alert("isSingleLine didn't quite work out.");
}

Play around with it in this fiddle 这个小提琴中玩耍

Update I now noticed that you don't want to encapsulate in a try-catch block. 更新我现在注意到,您不想封装在try-catch块中。 Using a try-catch block though can even handle errors inside the isSingleLine() method as shown in the updated fiddle . 不过,使用try-catch块甚至可以处理isSingleLine()方法内部的错误,如更新的fiddle所示。

(x.isSingleLine || function(){
    console.log("Doesn't Exist");
})()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vagrant和javascript错误可能是由于行尾 - Errors with Vagrant and javascript possibly due to line endings 我怎么能用JavaScript对它进行排序? - How can I possibly sort this in JavaScript? 如何使用javascript调用用代码编写的非静态方法? - How can I call a non static method written in code behind using javascript? 在javascript中如何在另一个原型方法中调用一个原型方法? - In javascript how can I call one prototype method in another prototype method? 是否可以动态调用不存在的方法然后创建它? - Is it possible to call a non-existent method on the fly and then create it? 在使用javascript模块模式时,如何从私有方法中调用公共方法? - How can i call a public method from within a private one when using the javascript Module Pattern? 如何在没有WebMethod或Updatepanel的情况下通过javascript调用服务器端c#方法? - How can I call a server side c# method via javascript without a WebMethod or Updatepanel? 如何将JS文件组合成一个没有错误的文件 - How can I combine JS files into one without errors 如何在不使程序崩溃的情况下检查不存在的变量的值? - How do I check the value of a non-existent variable without crashing the program? 如何在Javascript中设计自定义控件(可能使用jQuery) - How can I design a custom control in Javascript (possibly using jQuery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM