Adding custom properties to a function

后端 未结 10 742
独厮守ぢ
独厮守ぢ 2020-11-27 10:34

Searching for appropriate answer proved difficult because of the existence of many other problems related to my keywords, so I\'ll ask this here.

As we know, functio

相关标签:
10条回答
  • 2020-11-27 10:57

    It's perfectly acceptable to add properties or methods to a function object. It's done quite often. The jQuery/$ object is an example of this. It's a function with quite a few methods attached.

    When properties are added to a constructor they are called 'static' properties and can be invoked without an an instance of the class. e.g. Object.create.

    I don't have enough rep to write a comment so I will say here: It generally considered bad practice to extend the prototypes of built in objects, especially if your code has to play with other people's code. It can have unpredictable consequences that are hard to to track.

    0 讨论(0)
  • 2020-11-27 10:58

    First of all, it's important to realise that standard function properties (arguments, name, caller & length) cannot be overwritten. So, forget about adding a property with that name.

    Adding your own custom properties to a function can be done in different ways that should work in every browser.


    Adding your own custom properties to a function

    Way 1 : adding properties while running the function :

    var doSomething = function() {
        doSomething.name = 'Tom';
        doSomething.name2 = 'John';
        return 'Beep';
    };
    
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    console.log('doSomething() : ' + doSomething());
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    

    Output :

    doSomething.name : 
    doSomething.name2 : undefined
    doSomething() : Beep
    doSomething.name : 
    doSomething.name2 : John 
    

    Way 1 (alternate syntax) :

    function doSomething() {
        doSomething.name = 'Tom';
        doSomething.name2 = 'John';
        return 'Beep';
    };
    
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    console.log('doSomething() : ' + doSomething());
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    

    Output :

    doSomething.name : doSomething
    doSomething.name2 : undefined
    doSomething() : Beep
    doSomething.name : doSomething
    doSomething.name2 : John 
    

    Way 1 (second alternate syntax) :

    var doSomething = function f() {
        f.name = 'Tom';
        f.name2 = 'John';
        return 'Beep';
    };
    
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    console.log('doSomething() : ' + doSomething());
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    

    Output :

    doSomething.name : f
    doSomething.name2 : undefined
    doSomething() : Beep
    doSomething.name : f
    doSomething.name2 : John 
    

    A problem with this strategy is that you need to run your function at least once to assign the properties. For many functions, that's obviously not what you want. So let's consider the other options.


    Way 2 : adding properties after defining the function :

    function doSomething() {
        return 'Beep';
    };
        
    doSomething.name = 'Tom';
    doSomething.name2 = 'John';
    
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    console.log('doSomething() : ' + doSomething());
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    

    Output :

    doSomething.name : doSomething
    doSomething.name2 : John
    doSomething() : Beep
    doSomething.name : doSomething
    doSomething.name2 : John 
    

    Now, you don't need to run your function first before you're able to access your properties. However, a disadvantage is that your properties feel disconnected from your function.


    Way 3 : wrap your function in anonymous function :

    var doSomething = (function(args) {
        var f = function() {
            return 'Beep';
        };
        for (i in args) {
            f[i] = args[i];
        }
        return f;
    }({
        'name': 'Tom',
        'name2': 'John'
    }));
    
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    console.log('doSomething() : ' + doSomething());
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    

    Output :

    doSomething.name : 
    doSomething.name2 : John
    doSomething() : Beep
    doSomething.name : 
    doSomething.name2 : John 
    

    Wrapping your function in an anonymous function, you can collect your attributes into an object and use a loop to add those attributes one-by-one within the anonymous function. That way, your attributes feel more connected to your function. This technique is also very useful for when your attributes need to be copied from an existing object. A disadvantage, however, is that you can only add multiple attributes at the same time when you define your function. Also, it doesn't exactly result in DRY code if adding properties to a function is something you want to do often.


    Way 4 : add an 'extend' function to your function, that adds the properties of an object to itself one by one :

    var doSomething = function() {
        return 'Beep';
    };
        
    doSomething.extend = function(args) {
        for (i in args) {
            this[i] = args[i];
        }
        return this;
    }
    
    doSomething.extend({
        'name': 'Tom',
        'name2': 'John'
    });
    
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    console.log('doSomething() : ' + doSomething());
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    

    Output :

    doSomething.name : 
    doSomething.name2 : John
    doSomething() : Beep
    doSomething.name : 
    doSomething.name2 : John 
    

    This way, you can extend multiple properties and/or copy properties from another project at any time. Again, however, your code isn't DRY if this is something you do more often.


    Way 5 : Make a generic 'extend' function :

    var extend = function(obj, args) {
        if (Array.isArray(args) || (args !== null && typeof args === 'object')) {
            for (i in args) {
                obj[i] = args[i];
            }
        }
        return obj;
    }
        
    var doSomething = extend(
        function() {
            return 'Beep';
        }, {
            'name': 'Tom',
            'name2': 'John'
        }
    );
    
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    console.log('doSomething() : ' + doSomething());
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    

    Output :

    doSomething.name : 
    doSomething.name2 : John
    doSomething() : Beep
    doSomething.name : 
    doSomething.name2 : John 
    

    A genetic extend function allows for a more DRY approach, allowing you to add the object or any project to any other object.


    Way 6 : Create an extendableFunction object and use it to attach an extend function to a function :

    var extendableFunction = (function() {
        var extend = function(args) {
            if (Array.isArray(args) || (args !== null && typeof args === 'object')) {
                for (i in args) {
                    this[i] = args[i];
                }
            }
            return this;
        };
        var ef = function(v, obj) {
            v.extend = extend;
            return v.extend(obj);
        };
    
        ef.create = function(v, args) {
            return new this(v, args);
        };
        return ef;
    })();
    
    var doSomething = extendableFunction.create(
        function() {
            return 'Beep';
        }, {
            'name': 'Tom',
            'name2': 'John'
        }
    );
    
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    console.log('doSomething() : ' + doSomething());
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    

    Output :

    doSomething.name : 
    doSomething.name2 : John
    doSomething() : Beep
    doSomething.name : 
    doSomething.name2 : John 
    

    Rather than using a generic 'extend' function, this technique allows you to generate functions that have an 'extend' method attached to it.


    Way 7 : Add an 'extend' function to the Function prototype :

    Function.prototype.extend = function(args) {
        if (Array.isArray(args) || (args !== null && typeof args === 'object')) {
            for (i in args) {
                this[i] = args[i];
            }
        }
        return this;
    };
    
    var doSomething = function() {
        return 'Beep';
    }.extend({
        name : 'Tom',
        name2 : 'John'
    });
    
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    console.log('doSomething() : ' + doSomething());
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    

    Output :

    doSomething.name : 
    doSomething.name2 : John
    doSomething() : Beep
    doSomething.name : 
    doSomething.name2 : John 
    

    A great advantage to this technique is that it makes adding new properties to a function very easy and DRY as well as completely OO. Also, it's pretty memory friendly. A downside, however, is that it's not very future proof. In case future browsers ever add a native 'extend' function to the Function prototype, this that could break your code.


    Way 8 : Run a function recursively once and then return it :

    var doSomething = (function f(arg1) {
        if(f.name2 === undefined) {
            f.name = 'Tom';
            f.name2 = 'John';
            f.extend = function(args) {
                if (Array.isArray(args) || (args !== null && typeof args === 'object')) {
                    for (i in args) {
                        this[i] = args[i];
                    }
                }
                return this;
            };
            return f;
        } else {
            return 'Beep';
        }
    })();
    
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    console.log('doSomething() : ' + doSomething());
    console.log('doSomething.name : ' + doSomething.name);
    console.log('doSomething.name2 : ' + doSomething.name2);
    

    Output :

    doSomething.name : f
    doSomething.name2 : John
    doSomething() : Beep
    doSomething.name : f
    doSomething.name2 : John 
    

    Run a function once and have it test whether one of its properties is set. If not set, set the properties and return itself. If set, execute the function. If you include an 'extend' function as one of the properties, you can later execute that to add new properties.


    Adding your own custom properties to an object

    In spite of all these options, I would nevertheless recommend against adding properties to a function. It's much better to add properties to objects!

    Personally, I prefer the singleton classes with the following syntax.

    var keyValueStore = (function() {
        return {
            'data' : {},
            'get' : function(key) { return keyValueStore.data[key]; },
            'set' : function(key, value) { keyValueStore.data[key] = value; },
            'delete' : function(key) { delete keyValueStore.data[key]; },
            'getLength' : function() {
                var l = 0;
                for (p in keyValueStore.data) l++;
                return l;
            }
        }
    })();
    

    An advantage to this syntax is that it allows for both public and private variables. For example, this is how you make the 'data' variable private :

    var keyValueStore = (function() {
        var data = {};
        
        return {
            'get' : function(key) { return data[key]; },
            'set' : function(key, value) { data[key] = value; },
            'delete' : function(key) { delete data[key]; },
            'getLength' : function() {
                var l = 0;
                for (p in data) l++;
                return l;
            }
        }
    })();
    

    But you want multiple datastore instances, you say? No problem!

    var keyValueStore = (function() {
        var count = -1;
        
        return (function kvs() {
            count++; 
            return {
                'data' : {},
                'create' : function() { return new kvs(); },
                'count' : function() { return count; },
                'get' : function(key) { return this.data[key]; },
                'set' : function(key, value) { this.data[key] = value; },
                'delete' : function(key) { delete this.data[key]; },
                'getLength' : function() {
                    var l = 0;
                    for (p in this.data) l++;
                    return l;
                }
            }
        })();
    })();
    

    Finally, you can seperate the instance and singleton properties and use a prototype for the instance's public methods. That results in the following syntax :

    var keyValueStore = (function() {
        var count = 0; // Singleton private properties
            
        var kvs = function() {
            count++; // Instance private properties
            this.data = {};  // Instance public properties
        };
        
        kvs.prototype = { // Instance public properties
            'get' : function(key) { return this.data[key]; },
            'set' : function(key, value) { this.data[key] = value; },
            'delete' : function(key) { delete this.data[key]; },
            'getLength' : function() {
                var l = 0;
                for (p in this.data) l++;
                return l;
            }
        };
            
        return  { // Singleton public properties
            'create' : function() { return new kvs(); },
            'count' : function() { return count; }
        };
    })();
    

    With this syntax, you can have :

    • multiple instances of an object
    • private variables
    • class variables

    You use it like this :

    kvs = keyValueStore.create();
    kvs.set('Tom', "Baker");
    kvs.set('Daisy', "Hostess");
    var profession_of_daisy = kvs.get('Daisy');
    kvs.delete('Daisy');
    console.log(keyValueStore.count());
    
    0 讨论(0)
  • 2020-11-27 11:08

    It's a little bit difficult to give a very meaningful answer to your question, because you've sort of said "Here is my solution, is it OK?" without explaining what problem you are trying to solve (you even said explicitly that you are not going to explain the "why"). Your code looks to be valid JavaScript that will run, but it also looks like a less than optimal way of doing things.

    If you explain what you actually want to achieve you may get some good suggestions on better ways to structure your code. Still, I'll give you some kind of answer:

    Can this method be considered "proper" and standards compliant? It works in Firefox but there are many things working as expected in web browsers and aren't by any means standards.

    Functions are objects (as you've said), and thus it is possible to add properties to them. This isn't really a standards issue as such in that it is a core part of JavaScript that all browsers support.

    Is this kind of altering objects by adding new properties to them a good practice?

    It's your object, you can add whatever properties you like. The whole point of objects is that they have properties that you can manipulate. I can't really envisage a way of using objects that doesn't involve altering them, including adding, deleting and updating properties and methods.

    Having said that, to me it doesn't really make sense to add properties to the myMethod function, it would be more usual to add other properties to your something object (your myMethod function would, if called correctly, have access to the other properties of something via the this keyword).

    If you are using a function as a constructor it typically makes sense to add methods to the associated prototype and add (non-method) properties to each instance, but you can do either or both the other way when appropriate. (Noting that a "method" is essentially just a property that happens to reference a function.)

    The specific code you have shown doesn't add properties, it tests whether the someProperty property already exists and if so assigns it a new value.

    You might benefit from reading some articles such as these at MDN:

    • Working with Objects
    • Introduction to Object-Oriented JavaScript
    0 讨论(0)
  • 2020-11-27 11:12

    "necromancing" here, but I think every great question needs simple answers:

    Yes and Yes*

    By attaching the properties to the function you clean up the scope, improve readability and add logical cohesion. An added benefit is that you document the relationship between the function and the variables. I think that's a superior design, much better than adding variables on the scope some examples of attaching properties to instances of functions

    Created some fun examples here and here. HERE AND HERE


    * I think it's worth noting that you probably won't see this very often. most developers probably don't realize it's possible. Some people are crazy about every drop of performance... "JavaScript engines optimize based on the 'shape' of an object'..." blah blah blah... ut I think you can follow the rule you have for Objects and you'll do fine.

    0 讨论(0)
  • 2020-11-27 11:12

    I realize I'm years late to this, but thought I'd add this example--requirejs sets a property called "amd" on the define() function, which is quite handy as the UMD pattern uses it to detect that the define() function that's in scope is in fact an AMD define() function.

    RequireJS source: http://requirejs.org/docs/release/2.1.9/comments/require.js

    UMD pattern showing this usage: https://github.com/umdjs/umd/blob/master/amdWeb.js

    0 讨论(0)
  • 2020-11-27 11:13

    Attaching properties to functions is a beautiful (arguably sluggish/hack-ish) way of overloading the () operator, which in turn is usually used to implement functors: Object types that have one really important job, and all its other functionality (if there is any) is just a bunch of helpers. You could also interpret these functors as, basically, a "stateful" function where the state is public (most inline functions for example, have private state, that is state from the local scope).

    This JSFiddle demonstrates how we can use a function with custom properties for a translator function with additional utilities:

    /**
     * Creates a new translator function with some utility methods attached to it.
     */
    var createTranslator = function(dict) {
        var translator = function(word) {
            return dict[word];
        };
    
        translator.isWordDefined = function(word) {
            return dict.hasOwnProperty(word);
        };
    
        // Add more utilities to translator here...
    
        return translator;
    };
    
    
    // create dictionary
    var en2deDictionary = {
        'banana': 'Banane',
        'apple': 'Apfel'
    };
    
    // simple use case:
    var translator = createTranslator(en2deDictionary);
    var pre = $('<pre>');
    $("body").append(pre);
    
    pre.append(translator('banana') + '\n');
    pre.append(translator('apple') + '\n');
    pre.append(translator.isWordDefined('w00t') + '\n');
    

    As you can see, this is perfect for a translator whose sole purpose is to translate. Of course there are many more examples of theses types of objects, but they are by far not as common as types with diversified functionality, such as the classic User, Animal Car etc. types. To those sort of types, you only want to add custom properties in very few cases. Usually, you want to define those as more complete classes, and have their public properties reachable through this and it's prototype.

    0 讨论(0)
提交回复
热议问题