codesuck codesuck - 3 months ago 11
Javascript Question

JavaScript and jQuery code to return function by var

I am working on small api to get content from my database. I want to show data in a JavaScript/jQuery function according to variables I declared.

example

<script>
function dataset() {
var url = "www.example.com";
var name = "Mini Api";
ver version = "1.00";

this.return url;
this.return name;
this.return version;
}
</script>


How I intend to use it

<script>
minapi = new dataset();
//here I want to return only name
alert(minapi.name);
</script>


Please any idea of how to get this done?

In addition, is there a way to create jQuery function to execute once page load without using this
$( document ).ready(function() {});
?

Answer

I think you want this:

function dataset() {

   var myData = {
       url: "www.example.com",
       name: "mini api",
       version: "1.00"
   };

   return myData;

}

var myVar = dataset();
console.log(myVar.name);

JSFiddle: https://jsfiddle.net/1455d5z3/