Summary
The Object.prototype property represents the Object prototype object.
Property attributes of Object.prototype |
|
|---|---|
| Writable | No |
| Enumerable | No |
| Configurable | No |
Description
All objects in JavaScript are descended from Object; all objects inherit methods and properties from Object.prototype, although they may be overridden (except an Object with a null prototype, i.e. Object.create(null)). For example, other constructors' prototypes override the constructor property and provide their own toString methods. Changes to the Object prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain.
Properties
-
Object.prototype.constructor - Specifies the function that creates an object's prototype.
-
Object.prototype.__proto__ - Points to the object which was used as prototype when the object was instantiated.
-
Object.prototype.__noSuchMethod__ - Allows a function to be defined that will be executed when an undefined object member is called as a method.
-
Object.prototype.__count__ -
Used to return the number of enumerable properties directly on a user-defined object, but has been removed. -
Object.prototype.__parent__ -
Used to point to an object's context, but has been removed.
Methods
-
Object.prototype.__defineGetter__() - Associates a function with a property that, when accessed, executes that function and returns its return value.
-
Object.prototype.__defineSetter__() - Associates a function with a property that, when set, executes that function which modifies the property.
-
Object.prototype.__lookupGetter__() -
Returns the function associated with the specified property by the
__defineGetter__method. -
Object.prototype.__lookupSetter__() -
Returns the function associated with the specified property by the
__defineSetter__method. -
Object.prototype.hasOwnProperty() - Returns a boolean indicating whether an object contains the specified property as a direct property of that object and not inherited through the prototype chain.
-
Object.prototype.isPrototypeOf() - Returns a boolean indication whether the specified object is in the prototype chain of the object this method is called upon.
-
Object.prototype.propertyIsEnumerable() - Returns a boolean indicating if the internal ECMAScript DontEnum attribute is set.
-
Object.prototype.toSource() - Returns string containing the source of an object literal representing the object that this method is called upon; you can use this value to create a new object.
-
Object.prototype.toLocaleString() -
Calls
toString(). -
Object.prototype.toString() - Returns a string representation of the object.
-
Object.prototype.unwatch() - Removes a watchpoint from a property of the object.
-
Object.prototype.valueOf() - Returns the primitive value of the specified object.
-
Object.prototype.watch() - Adds a watchpoint to a property of the object.
-
Object.prototype.eval() -
Used to evaluate a string of JavaScript code in the context of the specified object, but has been removed.
Examples
Since Javascript doesn't exactly have sub-class objects, prototype is a useful workaround to make a "base class" object of certain functions that act as objects. For example:
var Person = function() {
this.canTalk = true;
this.greet = function() {
if (this.canTalk) {
console.log("Hi, I'm " + this.name);
}
};
};
var Employee = function(name, title) {
this.name = name;
this.title = title;
this.greet = function() {
if (this.canTalk) {
console.log("Hi, I'm " + this.name + ", the " + this.title);
}
};
};
Employee.prototype = new Person();
var Customer = function(name) {
this.name = name;
};
Customer.prototype = new Person();
var Mime = function(name) {
this.name = name;
this.canTalk = false;
};
Mime.prototype = new Person();
var bob = new Employee('Bob','Builder');
var joe = new Customer('Joe');
var rg = new Employee('Red Green','Handyman');
var mike = new Customer('Mike');
var mime = new Mime('Mime');
bob.greet();
joe.greet();
rg.greet();
mike.greet();
mime.greet();
This will output:
Hi, I'm Bob, the Builder
Hi, I'm Joe
Hi, I'm Red Green, the Handyman
Hi, I'm Mike
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition. Implemented in JavaScript 1.0 | Standard | Initial definition. |
| ECMAScript Language Specification 5.1th Edition (ECMA-262) | Standard | |
| ECMAScript Language Specification 6th Edition (ECMA-262) | Draft |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |