Home
<!DOCTYPE html> <html> <body> <h1>Demo: Read-only property</h1> <script> function Person(firstName) { var _firstName = firstName || "unknown"; Object.defineProperties(this, { "FirstName": { get: function () { return _firstName; } } }); }; var person1 = new Person("Steve"); //person1.FirstName = "James"; //will not work alert(person1.FirstName ); var person2 = new Person("Bill"); //person2.FirstName = "John"; // will not work alert(person2.FirstName ); </script> </body> </html>
Result: