Profesionalus interneto narkomanas • Žaidimų entuziastas • Technologijų kūrėjas
Profesionalus interneto narkomanas • Žaidimų entuziastas • Technologijų kūrėjas

Sukurkite getterius ir setterius naudodami JavaScript Object.defineProperty

Daugiau jokių „getters“ ir „setters“ funkcijų dėka „Object.defineProperty“ „JavaScript“ kalboje!
Šį puslapį iš anglų kalbos jūsų patogumui išvertė mano labai motyvuoti dirbtinio intelekto praktikantai. Jie vis dar mokosi, todėl galėjo būti praleista keletas klaidų. Norėdami gauti tiksliausią informaciją, žr. anglišką versiją.
Pradžia Tinklaraštis Sukurkite getterius ir setterius naudodami JavaScript Object.defineProperty

Atkreipkite dėmesį, kad šis tinklaraščio įrašas buvo paskelbtas 2013 m. balandžio mėn., todėl, priklausomai nuo to, kada jį skaitysite, tam tikros dalys gali būti pasenusios. Deja, ne visada galiu atnaujinti šiuos įrašus, kad užtikrinčiau informacijos tikslumą.

    Coming from 15 years of Java development, writing (probably thousands of) traditional setters and getters methods in various Java classes has been so deeply rooted in my backbone.
    /* * A Java class with traditional setters and getters */ public class Person { private String name; public void setName(final String name) { this.name = name; } public String getName() { return name; } }
    As a developer you write this because you want to maintain data encapsulation, meaning the only way to get the Person's name is through the public getName method. I could have made the name variable public directly as well, like this:
    /* * A Java class with only public variables */ public class Person { public String name; }
    However, by doing this it would not be possible to add extra functionality when someone retrieves the name, such as the returned name should be "John Doe" if no name has been set:
    /* * A Java class with extra functionality in the getter */ public class Person { private String name; public String getName() { return name == null ? "John Doe" : name; } }
    In JavaScript you could do the same thing (simple example, not using the prototype object):
    /* * A simple JavaScript example */ var Person = function() { var name; this.getName = function() { return this.name ? this.name : "John Doe"; } }; var someone = new Person(); console.log( someone.getName() );
    However, the problem (in my opinion) with getters and setters functions is the increased boilerplate code. Everywhere you constantly need to write:
    var name = person.getName();
    instead of directly and maybe more readable:
    var name = person.name;
    Now you maybe ask yourself, won't we have the same problem in JavaScript, like in Java, if we would like the "John Doe" functionality? Actually no - and this is thanks to the Object.defineProperty function in the JavaScript language.
    With Object.defineProperty you can define a property on an object and even define the functionality behind it when the property is read or assigned. Here is an example:
    /* * A JavaScript example using Object.defineProperty */ var Person = function() { }; Object.defineProperty(Person.prototype, "name", { get: function() { return this._name ? this._name : "John Doe"; } }); var someone = new Person(); console.log( someone.name );
    Please note that in the example above, I add the name property on the prototype object and not directly on the Person object (to increase performance).
    In my opinion this is really simple and very elegant. You decrease boilerplate code throughout your whole solution, but still achieve data data encapsulation - and that's very cool :-)

    Autorius: Special Agent Squeaky. Pirmą kartą publikuota 2013-04-01. Paskutinį kartą atnaujinta 2013-04-01.

    📺 Žiūrėkite Squeaky naujausią vaizdo įrašą!

    Kaip pridėti paprastus realaus laiko subtitrus į jūsų tiesioginę transliaciją