Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create javascript library of self-written functions [closed]

I have written a bunch of javascript functions in my html file between the tags but now I want all of my functions in a seperate JS file so I can re-use the JS file for other html pages so that I only need to include the JS file.

this is how my functions look like:

function makeStartRefresher(refresh, refreshTime) {
//function code
}
function readData(address){
//function code
}
function writeData(address, value, refreshCallback){....
........
........
........

These are written above my document.ready function, from where I call and use these functions.

Will it work if I just copy these functions to a JS file and include the JS file in my html document, will I be able to call the functions like normal?

Grtz

like image 240
Stijn De Schutter Avatar asked Apr 29 '13 13:04

Stijn De Schutter


People also ask

How can I create my own JavaScript?

To write a JavaScript, you need a web browser and either a text editor or an HTML editor. Once you have the software in place, you can begin writing JavaScript code. To add JavaScript code to an HTML file, create or open an HTML file with your text/HTML editor.

Does JavaScript allow us to write own functions as well?

JavaScript allows us to write our own functions as well. This section explains how to write your own functions in JavaScript.

Does JavaScript have built in libraries?

While other platforms, like Android or iOS, clearly state what a library is, what format it must have and how it should be integrated into a project, JavaScript doesn't provide any built-in mechanism.


2 Answers

Ordinary I use something like this to define separate module.

LibName= window.LibName || {};

LibName = function () {

  var yourVar1;
  var yourVar2;


  publicFunc1 = function() {

  };

  privateFunc2 = function() {

  };


  return {
    "publicFuncName" :  publicFunc1
  }

}();

In HTML it could be called like LibName.publicFuncName()

Also, look at Module Pattern here - http://addyosmani.com/resources/essentialjsdesignpatterns/book/

UPD:

Now ES6 is quite ready-for-production and the answer should be updated:

class MyLib {
  constructor() {
    this.foo = 1;
    this.bar = 2;
  }

  myPublicMethod1() {
    return this.foo;
  }

  myPublicMethod2(foo) {
    return foo + this.bar;
  }
}

const instance = new MyLib();
export { instance as MyLib };

And when you need it:

import { MyLib } from './MyLib';
like image 142
Dmitry Volokh Avatar answered Sep 21 '22 19:09

Dmitry Volokh


Just copy your JS functions into a .js file and include it like this in the <head> section of your HTML documents:

<script type="text/javascript" src="mylibrary.js"></script>

The document.ready event won't be fired before all scripts linked that way are loaded and executed, so all functions defined in it will be available when it happens.

To avoid nameclashes with other libraries, you can optionally put all your functions into a global object which serves as a namespace.

// mylibrary.js
var myLibrary = {

    makeStartRefresher: function(refresh, refreshTime) {
    //function code
    },
    readData: function(address){
    //function code
    }
    ...
}

and then when you use functions from your library, refer to them like this:

myLibrary.makeStartRefresher(refresher, 1000);
like image 23
Philipp Avatar answered Sep 20 '22 19:09

Philipp