简体   繁体   中英

Calling a function defined outside of the Javascript library

I am working on library. I was trying to modify it, so that it uses a custom player instead of the HTML5 player. So I replaced the function calls to play() etc with the calls to my custom player(say custFunc1() ). These calls are defined in a separate file: custPlayer.js .

So in my index.html file, I will first include the custPlayer.js file and then the built file.

However the problem is that while building the video.js package using grunt, I get the error that custFunc1 is not defined and thus grunt is not able to create the video.js library.

Now I was able to find out from a colleague that adding /* global custFunc1 */ at the beginning of the particular file in the package from where I was calling custFunc1 resolves the issue. The grunt build succeeds and it works fine.

So what I want to know is:

  1. How does this actually resolve the issue, since this is exactly like a comment in javascript, how does it treat this differently and understand that it indicating that the function definition will be present outside the library?

  2. Is the word global some sort of keyword in javascript?

  3. Are there other ways of achieving this apart from what I mentioned?

  4. On a slightly different note, I wanted to ask if is the rough equivalent of ?

Your javascript is being linted as part of your grunt process, if you look at the root of your project folder you should see a file like .jshintrc or something along those lines (different depending on the linter).

Your current settings means that the linter is going through your .js files one at a time and if it comes across a variable or function from another files it's throwing the error your seeing. You can either turn off this check or add custFunc1 to an array of known global variables. In jshint you do it like so - https://github.com/gruntjs/grunt-contrib-jshint#jshintrc

{
  "globals": {
    "custFunc1": true
  }
}

The globals will probably already be present in the file, so just add custFunc1: true to it.

Oh and to answer question 1 - the comment type syntax tells the linter to ignore it's settings for that current file, basically overriding the settings in the .jshintrc file.

2 - Yes it's a setting in jshintrc and your adding custFunc1 to it inside the file itself instead of globally in the .jshintrc file.

3 - Mentioned above.

4 - Never used maker but yes i believe its similar in that its a pre process tool

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM