Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Mutator method
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===JavaScript=== In this example constructor-function <code>Student</code> is used to create objects representing a student with only the name stored. <syntaxhighlight lang="javascript"> function Student(name) { var _name = name; this.getName = function() { return _name; }; this.setName = function(value) { _name = value; }; } </syntaxhighlight> Or (using a deprecated way to define accessors in Web browsers):<ref>{{Cite web|title=Object.prototype.__defineGetter__() - JavaScript {{!}} MDN|url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__|access-date=2021-07-06|website=developer.mozilla.org|language=en-US}}</ref> <syntaxhighlight lang="javascript"> function Student(name){ var _name = name; this.__defineGetter__('name', function() { return _name; }); this.__defineSetter__('name', function(value) { _name = value; }); } </syntaxhighlight> Or (using prototypes for inheritance and [[ES6]] accessor syntax): <syntaxhighlight lang="javascript"> function Student(name){ this._name = name; } Student.prototype = { get name() { return this._name; }, set name(value) { this._name = value; } }; </syntaxhighlight> Or (without using prototypes): <syntaxhighlight lang="javascript"> var Student = { get name() { return this._name; }, set name(value) { this._name = value; } }; </syntaxhighlight> Or (using defineProperty): <syntaxhighlight lang="javascript"> function Student(name){ this._name = name; } Object.defineProperty(Student.prototype, 'name', { get: function() { return this._name; }, set: function(value) { this._name = value; } }); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)