JS definesetter-automatically adds a set attribute (method) to the JS "class)

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]

For ease of understandingCodeAnd better distinguish between public and private variables. I usually use the set... method (... for some member variables) to assign values to class members. 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 the implementation code <br/> VaR _ definesetter = function (self, P, hdle) {<br/> // if the property is null or the JS considers it to be false, return <br/> If (! P |! Self | "function "! = Typeof self) <br/> return NULL; <br/> // force transformation, and set self to reference the function (class, of course, you can also add the Set Method of the attribute in the class, rather than the set method of the prototype attribute <br/> P = string (p); self = self. prototype; <br/> // if the member is not a function, add set .. method <br/> If ("function "! = Typeof self [p]) {<br/> var n = (function _ n (n) {<br/> N = string (N ); for (VAR I = 0; I <n I var letter = "N. substring (0, 1 ). touppercase (); "n =" N. substr (1); "if return NULL hdle self new function else> <br/> <input onclick =" runex ('runcode36045 ') "type =" button "value =" Running code "> <input onclick =" docopy ('runcode36045 ') "type =" button "value =" Copy code "> <input onclick =" dosave (runcode36045) "type =" button "value =" Save code "> [Ctrl + A select all note: to import external JS files, refresh the file before execution.] </n>

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.

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.