JavaScript Method Invocation (with fail check)

JS
S
JavaScript

Simple snippet of some ways of invoking methods in JavaScript. Some concepts: Coercion (if value is 0, null, undefined JS will cast to false), && Short-cut Logic

1// Dot notation
2if (sender.change){ sender.change(); }
3
4// in operator
5if ("change" in sender) { sender.change(); }
6
7// hasOwnProperty * this method does not check down the object's prototype chain
8if (Object.hasOwnProperty.call(sender, "change")) { sender.change(); }
9
10// hasOwnProperty  * this method does not check down the object's prototype chain
11if(sender.hasOwnProperty("change")){ sender.change(); }
12hasOwnProperty(sender, 'change') //

Created on 1/12/2018