Hướng dẫn check function exists javascript
My code is
However, sometimes my
I want to degrade gracefully because this is not the most important feature in my program. Any suggestions on how to make
sure that it exists and then only execute (None of the methods below except try catch one work)
animuson♦ 52.8k28 gold badges139 silver badges145 bronze badges asked Jun 25, 2009 at 4:10
Alec SmartAlec Smart 91.5k38 gold badges118 silver badges181 bronze badges 4 Try something like this:
or better yet (as per UpTheCreek upvoted comment)
j0k 22.3k28 gold badges77 silver badges86 bronze badges answered Jun 25, 2009 at 4:14
Andrew HareAndrew Hare 337k71 gold badges633 silver badges630 bronze badges 14 I had this problem.
However, a colleague pointed out to me that checking if it's Simpler:
Much cleaner and works great.
ashleedawg 19.4k7 gold badges69 silver badges100 bronze badges answered Feb 6, 2013 at 23:27
Misha NasledovMisha Nasledov 1,8931 gold badge14 silver badges18 bronze badges 3 Modern JavaScript to the rescue!
The
Optional Chaining syntax (
In the example above, if a If no If a answered Mar 3, 2020 at 15:57
7 How about:
e.g.
or as for your case:
See MDN docs. answered Feb 10, 2015 at 16:15
1 If you're using eval to convert a string to function, and you want to check if this eval'd method exists, you'll want to use typeof and your function string inside an eval:
Don't reverse this and try a typeof on eval. If you do a ReferenceError will be thrown:
answered May 27, 2011 at 21:50
dhulihandhulihan 10.8k8 gold badges38 silver badges45 bronze badges 2 Try
Or as an if:
Or with a return value, on a single line:
answered Aug 14, 2013 at 20:32
Mat CarlsonMat Carlson 5233 silver badges12 bronze badges Didn't see this suggested: me.onChange && me.onChange(str); Basically if me.onChange is undefined (which it will be if it hasn't been initiated) then it won't execute the latter part. If me.onChange is a function, it will execute me.onChange(str). You can even go further and do:
in case me is async as well. answered Jan 18, 2016 at 20:04
Samir AlajmovicSamir Alajmovic 3,2013 gold badges26 silver badges28 bronze badges 0 For me the easiest way :
answered Aug 27, 2018 at 12:21
Tu4n3rTu4n3r 4115 silver badges10 bronze badges 1
answered Apr 23, 2015 at 13:14
3
OR
answered Oct 11, 2018 at 6:49
2 Put double exclamation mark i.e !! before the function name that you want to check. If it exists, it will return true.
answered Dec 15, 2018 at 15:43
answered Jun 25, 2009 at 4:13
MiffTheFoxMiffTheFox 21k14 gold badges67 silver badges92 bronze badges I'll go 1 step further to make sure the property is indeed a function
answered Jun 25, 2009 at 8:04
AlexAlex 33k10 gold badges52 silver badges68 bronze badges I like using this method:
Usage:
answered Aug 13, 2015 at 17:08
David DouglasDavid Douglas 10.3k2 gold badges53 silver badges53 bronze badges I had the case where the name of the function varied according to a variable (var 'x' in this case) added to the functions name. This works:
answered Mar 18, 2016 at 14:02
0 If you're checking for a function that is a jQuery plugin, you need to use $.fn.myfunction
Evg 23.5k5 gold badges40 silver badges75 bronze badges answered Feb 15, 2017 at 15:24
Lucas BustamanteLucas Bustamante 14.4k7 gold badges82 silver badges81 bronze badges Here is a working and simple solution for checking existence of a function and triggering that function dynamically by another function; Trigger function
and you can now generate the function dynamically maybe using php like this
now you can call the function using dynamically generated event
the exact HTML code you need is
Brian C 8181 gold badge9 silver badges20 bronze badges answered Mar 23, 2018 at 7:59
In a few words: catch the exception. I am really surprised nobody answered or commented about Exception Catch on this post yet. Detail: Here goes an example where I try to match a function which is prefixed by mask_ and suffixed by the form field "name". When JavaScript does not find the function, it should throw an ReferenceError which you can handle as you wish on the catch section.
marc_s 714k171 gold badges1315 silver badges1433 bronze badges answered Oct 24, 2019 at 18:24
With no conditions
answered Jun 25, 2009 at 4:19
I would suspect that Moving the get_ID call into the onclick event should take care of it. Obviously you can further trap as previously mentioned:
answered Jun 25, 2009 at 4:51
wombletonwombleton 8,2461 gold badge28 silver badges30 bronze badges I always check like this:
just place it before any code that uses this function answered Sep 21, 2013 at 11:57
el Dudeel Dude 4,7135 gold badges26 silver badges39 bronze badges This simple jQuery code should do the trick:
answered Nov 18, 2016 at 15:03
I have tried the accepted answer; however:
returns 'undefined'. I've noticed that the specification states an event called 'onchange' instead of 'onChange' (notice the camelCase). Changing the original accepted answer to the following worked for me:
answered Mar 1, 2017 at 16:57
I have also been looking for an elegant solution to this problem. After much reflection, I found this approach best.
answered Jul 27, 2019 at 9:29
JulianJulian 2362 silver badges14 bronze badges 2 I would suggest using:
The first check
Checking for only a truthy value isn't enough, since After validating that Applying this to your code you can now do:
answered Nov 3, 2020 at 13:27
3limin4t0r3limin4t0r 17.2k2 gold badges24 silver badges47 bronze badges
answered May 17, 2017 at 5:05
Ir CalifIr Calif 4666 silver badges7 bronze badges And then there is this...
answered Dec 16, 2017 at 12:49
Master JamesMaster James 1,53514 silver badges19 bronze badges 4 Try this one:
Be aware that I've write this with my cellphone Might contain some uppercase issues and/or other corrections needed like for example functions name If you want a function like PHP to check if the var is set:
answered May 1, 2018 at 13:10
To illustrate the preceding answers, here a quick JSFiddle snippet :
answered Jun 12, 2018 at 17:43
HoCo_HoCo_ 1,2024 gold badges15 silver badges31 bronze badges |