The following example is one way in JavaScript to create and manipulate objects, i.e. with the new Object()
syntax. The other way is by creating an object literal.
I remember reading somewhere but can't find it now that using "new Object()" to create objects in JavaScript should be avoided for some reason.
Is there a reason now to use new Object()
as in the following code?
<html>
<head>
<title>Test Page</title>
<script type="text/javascript">
window.onload = function() {
var layout = new Object();
layout.idCode = 'simple';
layout.title = 'Simple Layout';
layout.content = '';
layout.width = 400;
layout.display = function() {
return '<div style="background-color: #eee; width:'+this.width+'">'+this.content+'</div>'
};
layout.width = 200;
layout.content = 'This is the new content';
document.getElementById('output').innerHTML = layout.display();
};
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>