There is a piece of code that I am confused about
var clickEventType=((document.ontouchstart!==null)?'click':'touchstart');
If it says document.ontouchstart!==null
, it means that touchstart exists, and it should look like this:
var clickEventType=((document.ontouchstart!==null)?'touchstart':'click')
Why is it written the way above?
If written as
You will understand,
ontoucstart
is an attribute of thedoucment
object, butontouchstart
points to a reference to the function, that is,ontouchstart
points to an object. When it is not pointed to,ontouchstart
needs an initial value. This initial value isnull
. If thetouchstart
event is supported, then the initial value of ontouchstart is set tonull
, so it is written like thisThis shows that the current browser supports ontouchstart, because if it does not support it, the value of
document.ontouchstart
isundefined
, and if
document.ontouchstart
is not bound to the event, it is equal to null, so you You can use it like thisdocument.ontouchstart = function(){};
The initial value is Null. If it is not equal to null, it can be said that it is not supported