Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <h2>JavaScript Object Constructors</h2> <p id="demo"></p> <script> // Constructor function for Person objects function Person(firstName,lastName,age,eyeColor) { this.firstName = firstName; this.lastName = lastName; this.age = age; this.eyeColor = eyeColor; this.changeName = function (name) { this.lastName = name; } } // Create a Person object const myMother = new Person("Sally","Rally",48,"green"); // Change last name myMother.changeName("Doe"); // Display last name document.getElementById("demo").innerHTML = "My mother's last name is " + myMother.lastName; </script> </body> <!-- Mirrored from www.w3schools.com/js/tryit.asp?filename=tryjs_object_constructor7 by HTTrack Website Copier/3.x [XR&CO'2014], Mon, 05 Sep 2022 14:46:49 GMT --> </html>