Js defineSetter-automatically adds a set attribute (

Source: Internet
Author: User

Writing comments is not my strength. If you have any questions, please write them in the comments: D

When writing a JS class, such

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
To make it easy to understand the code and better distinguish between public and private variables, I usually use set... method (... assign a value to a member of the class. These set methods are of course public. Another point is to make the code more standardized.

However, the problem lies here. If the above Code does not consider verification effectiveness (or simply verifies the effectiveness), we will have a lot more code, suppose a class has 20 members (attributes), so we have to add 20 sets... methods are redundant. Therefore, we need to solve this problem.

Recall that in Mozilla, there are _ defineSetter _ and _ defineGetter _ methods to add members to the DOM, which brings a lot of convenience to developers. We will also simulate a js version of _ defineSetter.

Simple thinking:
Dynamically add methods (or attributes) to objects using JS)
However, you do not need to add the set... method to the class.
The set... method is not added for attributes other than the [a-z] range.

Write implementation code
Var _ defineSetter = function (self, p, hdle) {// if the attribute is empty or the value in js is false, if (! P |! Self | "function "! = Typeof self) return null; // force transformation, and set self as a reference of this Function (that is, class). Of course, you can also add the Attribute set Method in the class, set Method p = String (p); self = self. prototype; // if the member is not a function, add set .. method if ("function "! = Typeof self [p]) {var n = (function _ n (n) {n = String (n); for (var I = 0; I <n. length; I ++) {var letter = n. substring (0, 1 ). toUpperCase (); n = n. substr (1); if (/[a-z]/I. test (letter) return 'set' + letter + n ;}; return null ;}) (p); // get set... this member is added dynamically for the member. If there is no custom method, the function (v) {this [p] = v} method is added; if (n & hdle & "function" = typeof hdle) self [n] = new Function ("v", "this. "+ p +" = "+ hdle + ". apply (this, [v]); "); else self [n] = new Function (" v "," this. "+ p +" = v; "); return self [n];} return self ;};
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The _ defineSetter is basically implemented, and it is not too troublesome for us to have defineSetter one by one. Now that the prototype has been implemented, use the prototype to dynamically bind it to the Function object, and a line of code will solve the set... method.
Function. prototype. defineAllSetter = function (hdle ){

For (var I in this. prototype)
_ DefineSetter. apply (this, [this, I, hdle]);

Return this;

};

Next, bind a defineSetter to the Function object.

Function. prototype. defineSetter = function (p, hdle ){

Return _ defineSetter. apply (this,
[This]. concat (Array. prototype. slice. call (arguments, 0 )));

};

OK! Basically complete the desired function. Try it...

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute] This DEMO and all the code:
Http://www.never-online.net/code/js/defineSetter/

Of course, we can also add verification ~, I will not write more specific code, but I have already implemented it. If you are interested, try to play: D.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.