Home  >  Article  >  Web Front-end  >  js defineSetter - Automatically add a set attribute (method) to js "class"_javascript skills

js defineSetter - Automatically add a set attribute (method) to js "class"_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:12:511084browse

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

When writing a JS class, such as


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

In order to easily understand the code, and To better distinguish public and private variables, I usually use the set... method (...for certain (some) member variables) to assign values ​​to members 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 validity (or only verifies simple validity), then we will have a lot of extra code. Imagine a class with 20 members (attributes) ), then we have to add 20 set... methods, which is really redundant, so we need to find a way 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. Let’s also imitate a js version of __defineSetter.

Simple idea:
Use JS to dynamically add methods (or attributes) to objects
But there is no need to add set... methods in the class.
Attributes that are not in the [a-z] range will no longer add set... methods.

Write the implementation code
]

Basically implemented _defineSetter, It would be too troublesome for us to defineSetter one by one. Now that the prototype has been implemented, use the prototype to dynamically bind to the Function object. One 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;

};

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! The desired function is basically completed. try it...

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute it
]
DEMO and all codes of this example:
http://www.never-online.net/code/js/defineSetter/ <script> function jsclass() {}; jsclass.prototype = { value: "never-online", //属性1 URL: "www.url.net", //属性2 setValue: function (v) { //set方法 this.value = v; }, setURL: function (v) { //set方法 if (/^www\.[a-z]|\-{2,}\.net|com$/i.test(v)) { this.URL = v; } }, print: function () { //打印 alert(this.value); alert(this.URL); } } var o = new jsclass(); o.setValue("never-online'blog"); o.setURL("www.never-online.net"); o.print() </script>Of course, we can also add verification~, I won’t write more specific code, haha, it has been implemented , friends who are interested should also try to play it :D.​
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn