Refti
Refti

Reputation: 755

Identifying a Javascript object

I am having a problem identifying a javascript object

function A(x, y){

    this.boo = function(){
    }
}
var aa = new A("1", "2");
aa.boo();

In the code, is 'aa' a javascript object? Does it inherit properties from object.prototype too? If yes, how can I be sure (from a novice).

Upvotes: 1

Views: 164

Answers (5)

RobG
RobG

Reputation: 147363

You can be sure because every native object inherits from Object.prototype (except for the Global object, whose [[Prototpye]] is implementation dependant, but it is an instance of Object in most browsers).

Edit

Some clarifications.

Native objects are those constructed using ECMAScript, e.g.

function foo() {}
var obj = {};
var arr = [];

and so on. They all have Object.prototype on their [[Prototype]] chain, which makes them instances of Object (and whatever other constructor's prototype is on the chain). An ECMAScript object can inherit from more than one prototype and hence be an instance of more than one object:

function Foo(){}
Foo instanceof Function; // true
Foo instanceof Object;   // true

var foo = new Foo();
foo instanceof Foo;     //true
foo instanceof Object;  //true

Built–in objects are those supplied by ECMAScript, e.g.

Global   instanceof Object; // might be true
Array    instanceof Object; // true
Math     instanceof Object; // true
Object   instanceof Object; // true - Object is an Object :-)
Date     instanceof Object; // true
Function instanceof Object; // true

and so on. Most of the above are also instances of Function (including Function).

Host objects are those supplied by the host environment, such as everything in the DOM, e.g.

document
document.createElement
window
var element = document.createElement('img');
var nodeList = element.getElementsByTagName();

and so on. These object only have to follow the most basic rules of ECMAScript, they don't need to have any inheritance model and may even throw errors when simply tested with instanceof or typeof operators (which is a nasty behaviour but some versions of some browsers do - many IE host objects implemented as ActiveX controls do, hence their creation within try..catch blocks).

Read the section on "Native ECMAScript Objects" (ES 3) and "Standard Built-in ECMAScript Objects" (ES 5) objects in the specification (ECMA–262), it's all there.

Upvotes: 4

T.J. Crowder
T.J. Crowder

Reputation: 1074198

In the code, is 'aa' a javascript object?

Yes. Or more accurately, aa is a variable that refers to a JavaScript object.

Does it inherit properties from object.prototype too?

From Object.prototype, yes. (Capitalization matters in JavaScript, because it's case-sensitive.)

If yes, how can I be sure (from a novice).

I'm not quite certain what you mean here. In JavaScript environments, there are host objects (Section 4.3.8 of the spec) and native objects (Section 4.3.6). Native objects all inherit from Object.prototype. Host objects may or may not.

There are lots of ways to query an object to find out what it "is". I outline several of them in my blog post "Say what?", but to summarize:

  • There's typeof, but regarding objects all it says is "object" (host or native), so that's not terribly useful if you want to know more about what kind of object it is.
  • There's instanceof, which can be used to check if an object was created via a specific constructor function (e.g., if (obj instanceof Date)).
  • There's a trick using Object.prototype.toString, which can sometimes give you more specific information about what kind of native object you're dealing with, if it's a built-in object (Section 4.3.7).
  • Or you can stop worrying and learn to love the duck. E.g., don't worry about what the object is, just look to see if it looks like it has/does what you want ("duck typing").

Upvotes: 1

Matt R. Wilson
Matt R. Wilson

Reputation: 7575

This is what I use

if( Object.prototype.toString.call( aa ) === '[object Object]' )
{
    doSomething();
}

Upvotes: 0

jfriend00
jfriend00

Reputation: 707258

Combining the previous two comments:

http://jsfiddle.net/jfriend00/x58w8/

function A(x, y){

    this.boo = function(){
    }
}
Object.prototype.foo = function() {};
var aa = new A("1", "2");
aa.boo();

alert(typeof aa);       // alerts object
alert(typeof aa.foo);   // alerts function

Upvotes: 0

CassOnMars
CassOnMars

Reputation: 6181

You can check its type:

if (typeof(aa) == "object") {
    alert("It's an object!");
}

which will cause an alert, meaning that it is an object, and also have any methods extended via object.prototype

Upvotes: 0

Related Questions