JS Definesetter-to JS "class" Automatically add a set of attributes (method) _javascript Tips

Source: Internet
Author: User
Writing a comment is not my strong point, please write it in the comments: D

When writing a JS class, such as
<script> function Jsclass () {}; Jsclass.prototype = {value: "Never-online",//property 1 URL: "Www.url.net",//Property 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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

To make it easier to understand the code, and to better differentiate between public and private variables, I usually use set ... Method (...). is assigned to a member of a class (some) member variable). These set methods are of course public. Another thing is to make code more normative.

The problem here, however, is that if the above code doesn't consider validation (or just simple validation), we'll have a lot more code, imagine a class with 20 members (attributes), then we have to add 20 set ... method, it is superfluous, so we must find a way to solve this problem.

In Mozilla, there are __definesetter__ and __definegetter__ ways to add members to the DOM and make it much easier for developers. We also have to imitate a JS version of the __definesetter.

Simple Way of thinking:
Use JS to dynamically add methods (or attributes) to an object
However, the methods in the class do not have to add the set ... Such methods.
Properties that are not [A-z] are no longer added set ... Such methods.

Write implementation Code
<textarea id= "runcode2718" > var _definesetter = function (self, p, hdle) {//If the property is null or value that is considered false in JS, then return if (!p | |!s Elf | | "function"!=typeof self) return null; Forced transformation and set self as a reference to the function (that is, Class), and of course you can add the property set method in the class, not the Set method p = String (p) of the prototype property; self = self.prototype; If the member is not a method (function), add the Set ... method if ("function"!=typeof self[p]) {var n = (function _n (n) {n = String (n); for (var i=0; I<n.leng Th i++) {var letter = n.substring (0,1). toUpperCase (); n = n.substr (1); if (/[a-z]/i.test) return ' set ' +letter+n; }; return null; }) (P); Get Set ... This member, the following is a dynamically added method for the member, and if there is no custom method, add the method of the function (v) {this[p]=v}; 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; }; </textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Basically realized the _definesetter, and we have to definesetter is not too much trouble. Now that the prototype is implemented, a single line of code solves the set by dynamically binding to a function object with a prototype. Way out.
Function.prototype.defineAllSetter = function (hdle) {

for (var i in This.prototype)
_definesetter.apply (this, [This,i,hdle]);

return this;

};

The next step is to 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) return ' set ' +letter+n; }; return null; }) (P); if (n && hdle && function "==typeof hdle) self[n] = new function (" V "," this. "+p+" = "+hdle+". A Pply (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.P Rototype.slice.call (arguments,0))); }; function Jsclass () {}; Jsclass.prototype = {value: ' Never-online ', URL: ' http://', blog: ' Blog ', Name: ' Jsclass ', Println:function () {for (var i) This.write (i + ": + This[i] +")}, Write:funct Ion (value) {Document.writeln (value); } }; Jsclass.defineallsetter (); Jsclass.definesetter (' Value ', function (ARG) {if ("a" ===arg) return "absolute a"; Return ' not a '; 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 All SELECT Note: If the need to introduce external JS need to refresh to perform]
This example demo and all code:
http://www.never-online.net/code/js/defineSetter/

Of course, we can also add verification ~, the specific code I will no longer write, oh, has been achieved, interested friends also 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.