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?
Test Pagewindow.onload = function() {
var layout = new Object();
layout.idCode = 'simple';
layout.title = 'Simple Layout';
layout.content = '';
layout.width = 400;
layout.display = function() {
return '
};
layout.width = 200;
layout.content = 'This is the new content';
document.getElementById('output').innerHTML = layout.display();
};
解决方案
It's just a bit ugly. Write this instead:
window.onload = function() {
var layout = {
idCode: 'simple',
title: 'Simple Layout',
content: '',
width: 400,
display: function() {
return '
},
width: 200,
content: 'This is the new content'
};
document.getElementById('output').innerHTML = layout.display();
};
which does the same thing.