JsdefineSetter-automatically adds a set attribute (method) _ javascript technique to the js "class"

Source: Internet
Author: User
JsdefineSetter-adding a set attribute (method) to js's & quot; class & quot; To write comments is not my strength. If you have any questions, please write them in the comments: D

When writing a JS class, such

Script function jsclass () {}; jsclass. prototype = {value: "never-online", // attribute 1 URL: "www.url.net", // attribute 2 setValue: function (v) {// set method this. value = v ;}, setURL: function (v) {// set method if (/^ www \. [a-z] | \-{2 ,}\. net | com $/I. test (v) {this. URL = v ;}, print: function () {// print alert (this. value); alert (this. URL) ;}} var o = new jsclass (); o. setValue ("never-online 'blog"); o. setURL ("www.never-online.net"); o. print () script
[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...

<Script type = "text/javascript"> // <! [CDATA [// power by never-online.net var _ defineSetter = function (self, p, hdle) {if (! P |! Self | "function "! = Typeof self) return null; p = String (p); self = self. prototype; if (p & "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 ); 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;}; Function. prototype. defineAllSetter = function (hdle) {for (var I in this. prototype) _ defineSetter. apply (this, [this, I, hdle]); return this ;}; Function. prototype. defineSetter = function (p, hdle) {return _ defineSetter. apply (this, [this]. concat (Array. prototype. slice. call (arguments, 0) ;}; function jsClass () {}; jsClass. prototype = {value: 'Never-online', URL: 'http: // ', blog: 'blog', name: "jsclass", println: function () {for (var I in this) this. write (I + ":" + this [I] + "")}, write: function (value) {document. writeln (value) ;}}; jsClass. defineAllSetter (); jsClass. defineSetter ('value', function (arg) {if ("a" = arg) return "Absolute a"; return "not ";}) var o = new jsClass (); o. setValue ('A'); o. setURL (" http://www.never-online.net "); O. setName ("never-online"); o. setBlog ("never-online's blog"); o. println (); //]> script
[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.

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.