tags:

views:

100

answers:

3

How to create my own JavaScript object and how can I check whether my custom object is present in my page or not?

What is the difference between JavaScript function calling using an ordinary JS file and creating a custom JavaScript object?

For Example:
We can check the Jquery object by using the following JavaScript code:

if(window.Jquery != 'undefined'){
    // Jquery Object is Present
}
A: 

How to create my own JavaScript object

var foo = {};

and how can I check whether my custom object is present in my page or not?

if (foo) { /* … */ }

What is the difference between JavaScript function calling using an ordinary JS file and creating a custom JavaScript object?

A function cannot "call a file". It could dynamically generate an HTML script element with a src attribute, and append it to the page. That would cause the resource to be loaded as normal.

We can check the Jquery object by using the following JavaScript code:

No. That would test to make sure that whatever window.Jquery was, it was not the string with the value "undefined".

David Dorward
A: 

You can create a JS object using JSON:

var someObject = {
    property1: 'my first property',
    property2: 'my second property',
    property3: 12345,
    property4: ['first list item','second list item']
};

Using Dojo, you could create an object as follows:

dojo.declare('gumbo.objects.first',null,{
    property1: 'my first property',
    property2: 'my second property'
});

There are many other ways of doing this, it all depends on what your preferences are and which libraries you use.

Emmster
That *ain't* JSON. That's just a JavaScript object literal.
Roatin Marth
… and not one that would conform to the JSON specification if it was string data.
David Dorward
You're right. Using JSON it would bevar someObject = { "property1" = "my first property"};
Emmster
@Emmster: no, using JSON it would be `var someObject = eval('({ "property1": "my first property" })')`. Get it? JSON is a data interchange format (think *"string"*). It happens to be legal notation for a JavaScript object literal.
Crescent Fresh
That's not what http://www.json.org/js.html implies, unless I completely misunderstand it.
Emmster
Never mind... I should have read past the first paragraph.
Emmster
+3  A: 

You create JavaScript objects/namespaces like this:

var FooQuery = {
  hello: function() { alert('hello') }
};

FooQuery.hello(); // alerts "hello"

Or like this (to have FooQuery instances):

function FooQuery() {
  this.hello = function() { alert('hello') };
}

new FooQuery().hello(); // alerts "hello"

Or like this:

function FooQuery() {}
FooQuery.prototype.hello = function() { alert('hello') };

new FooQuery().hello(); // alerts "hello"

You check whether it's available like this:

if(window.FooQuery) { /* ... */ }

Or like this:

if(typeof window.FooQuery !== 'undefined') { /* ... */ }
Roatin Marth
This is the way Doug Crockford does it, so it can't be bad! +1
AutomatedTester
@AutomatedTester: that statement does not compute.
Roatin Marth
@Roatin: lol...
Crescent Fresh