The main problem is that box
doesn't create a DOM element. You can't just append any object to the DOM, it has to be a DOM node of some kind (element, text node, etc.).
If you did have box
create a DOM element, you wouldn't want to store width
, height
, and color
on it directly, just in its style
object.
Since the object created when you use new
will get thrown away, I'd use createBox
instead and not use new
with it:
function createBox(width, height, backgroundColor) {
const element = document.createElement("div");
element.style.width = width + "px";
element.style.height = height + "px";
element.style.backgroundColor = backgroundColor;
return element;
}
document.body.appendChild(
createBox(100, 100, "red")
);
If you weren't wanting to create a DOM element, then the problem with this.style.width=width
is that it's trying to assign to undefined
, because this
doesn't have a style
property, so this.style
is undefined
. If you want to create an object from various properties, you can use an object literal:
function Box(width, height, color) {
this.width = width;
this.height = height;
this.color = color;
this.style = {width, height, backgroundColor: color};
}
(Note that I changed box
to Box
. The overwhelming convention in JavaScript is that constructor functions (ones you call via new
or similar) start with an uppercase character.)
But if you're creating a DOM element instead, it'll have a style
object already.
I should note that I've used shorthand properties for width
and height
there, which were added in ES2015 and so nearly universally supported, but not by obsolete browsers like IE11. If you need to support them, use the long form instead (width: width
).
You might also look in to ES2015's class
syntax, which lets you create constructor functions and fill in the object they assign as the prototype of instances in a more concise, less error-prone way. If you need to target ES5-only browsers, there are tools like Babel to transpile for you.
box1
is a not a DOM element so you can't append it to another DOM element – Sandy