Object is used by Prototype as a namespace and to call related function using Object which can be done in two ways.
- Simple developers can use the existing functions such as inspect or clone.
- If user wants to create his own objects like prototypes or explore the objects if they are hashes it will turn to extend keys and values.
The Prototype relation between two objects is defined as inheritance in each object have one more object just like as its prototype. Then the object inherit all properties of prototypes. Object defines its prototype by internal proprty [[Protoype]]. Each object have its own property, yet it can be null. The chain of object associated by the [[Prototype]] property is known as the prototype chain as shown in below image.
Following are some Object methods are briefly explained.
clone()
Is used to clones the passed object by using the shallow copy which copies all original properties to the result. the syntax as shown below.
[code]
object.clone();
[/code]
The code below demonstrates the clone() a shown below.
[jscript]
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showResult() {
var o = { name: 'Prototype', version: 1.5, authors: ['sam', 'contributors'] };
var o2 = Object.clone(o);
o2.version = '1.5 weird';
o2.authors.pop();
alert( " Value of o.version : " + o.version );
// Returns 1.5
alert( " Value of o2.version : " + o2.version );
// Returns 1.5 weird
alert( " Value of o.authors : " + o2.authors );
// Returns ['sam']
}
</script>
</head>
<body>
<p>Click the button to see the result.</p>
<br />
<br />
<input type = "button" value = "Result" onclick = "showResult();"/>
</body>
</html>
[/jscript]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.
extend()
Is used to copy all properties from sources to destination objects which is used by prototype to simulate inheritance by copying to prototypes. The syntax as shown below.
[code]
Object.extend(dest, src);
[/code]
The code below demonstrates the extend() as shown.
[jscript]
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showResult() {
var o1 = { name: 'Prototype', version: 1.5 };
// An empty object
var o2 = {};
var o2 = Object.extend(o2, o1);
alert( " Value of o2.name :" + o2.name );
// Returns Prototype
alert( " Value of o2.version :" + o2.version );
// Returns 1.5
}
</script>
</head>
<body>
<p>Click the button to see the result.</p>
<br />
<br />
<input type = "button" value = "Result" onclick = "showResult();"/>
</body>
</html>
[/jscript]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.
inspect()
inspect() is used to returns true if obj is an array otherwise which returns false the syntax as shown below.
[code]
Object.isArray(obj);
[/code]
The code below demonstrates the inspect() as shown.
[jscript]
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showResult() {
var o1 = [1,2,3,4,5];
var o2 = {name:"prototype", version:1.6};
alert( "Object.isArray(o1): " + Object.isArray(o1) );
alert( "Object.isArray(o2): " + Object.isArray(o2) );
}
</script>
</head>
<body>
<p>Click the button to see the result.</p>
<br />
<br />
<input type = "button" value = "Result" onclick = "showResult();"/>
</body>
</html>
[/jscript]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.