Home  >  Article  >  Web Front-end  >  Methods and codes for obtaining external function names in js classes_js object-oriented

Methods and codes for obtaining external function names in js classes_js object-oriented

WBOY
WBOYOriginal
2016-05-16 19:09:011131browse

For example, if we want to set a method in a class, we can call a method and save it in a class variable. When needed, we can get it by accessing the class variable.
Usually if we generate an instance
such as: var temp=new TopnetTree();
If we pass in a method by setting attributes, we will find that what is passed in is the content of a function. rather than the function name.
For example, temp.fileAction=fnTest; //fnTest is a function

So I wrote a method to implement this function.
Judge the incoming content through arguments:
Achieve the following functions:
The incoming content is empty and nothing is executed
Passing in a parameter means it is a parameterless function
If multiple parameters are passed in, it means that the first parameter is the function name, and the following parameters are each parameter.

Copy code The code is as follows:

1 TopnetTree.prototype.setFileAction=function( ){
2 var fnName,fnArgs="";
3 if(arguments.length==0){
4 return 0;
5 }else if(arguments.length==1) {
6 fnName=arguments[0];
7 }else{
8 fnName=arguments[0];
9 for(var i=1;i10                    fnArgs ="," arguments[i];                        
15 this.fileAction=fnName "(" fnArgs ")";
16 }

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