0

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
}

4 Answers 4

4

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') { /* ... */ }
Sign up to request clarification or add additional context in comments.

2 Comments

This is the way Doug Crockford does it, so it can't be bad! +1
@AutomatedTester: that statement does not compute.
0

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".

Comments

0

You can create your own JavaScript object given by below.

<script type="text/javascript">
objPerson    =    new Object();
objPerson.fname    =    "Milap";
objPerson.lname    =    "Patel";
objPerson.age    =    "24";
document.write(objPerson.age);
</script>

objectname = new Object(); will create JavaScript object named "objectname".

fname and lname are methods and we can assign values to object, objPerson.age = "24";

Comments

-1

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.

5 Comments

That ain't JSON. That's just a JavaScript object literal.
… and not one that would conform to the JSON specification if it was string data.
You're right. Using JSON it would be var someObject = { "property1" = "my first property" };
@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.
That's not what json.org/js.html implies, unless I completely misunderstand it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.