2

当我意识到我制作的整个 util obj 中只有一个函数需要返回时,我正在练习跨浏览器代码,我想知道为什么其他函数不需要返回。据我了解,当没有调用 return 时,JavaScript 将返回 undefined ,除非它是构造函数,在这种情况下它将返回 this 。

我用 util 中的所有函数对它进行了测试,它返回了每个条件的结果,并且它起作用了。然后我在条件的任何结果中都没有返回来测试它,但是如果没有在 getTarget 函数中返回,它就无法工作。

有人可以帮助我更好地理解其原因吗?

jsFiddle 在这里http://jsfiddle.net/Sus ​​annDelgado/CnWzE/

    var evtUtil = {
    //cross browser Events
    addEvent: function (el, type, fn) {
        if (typeof addEventListener !== "undefined") {
            el.addEventListener(type, fn, false);
        } else if (typeof attachEvent !== "undefined") {
            el.attachEvent("on" + type, fn);
        } else {
            el["on" + type] = fn;
        }
    },
    removeEvent: function (el, type, fn) {
        if (typeof removeEventListener !== "undefined") {
            el.removeEventListener(type, fn, false);
        } else if (typeof detachEvent !== "undefined") {
            el.detachEvent("on" + type, fn);
        } else {
            el["on" + type] = null;
        }
    },
    getTarget: function (event) {
        if (typeof event.target !== "undefined") {
            return event.target;
        } else {
            return event.srcElement;
        }
    },
    preventDefault: function (event) {
        if (typeof event.preventDefault !== "undefined") {
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
    }
};
4

2 回答 2

4

Return is used to pass something out of the function to the code that called it (generally speaking). Therefore in a function that doesn't need to pass anything back, such as removeEvent which is simply modifying the state of something else, it's not necessary.

The difference with getTarget is that as it says in the name, you're trying to retrieve a thing (in this case the event's target object). So in the code that calls getTarget, "something" is expected. As you say, without the return, JS simply passes back undefined.

于 2013-06-05T23:07:18.707 回答
4

You only need to declare return when you want the function to have an actual return value, whether it be a string, boolean, object, function, etc. If the function operates on something else, like adding an event handler or adding properties to something else, the return value is not needed. You could return this to be able to chain the method.

// An explicit return value
function getSomething() {
  return 'someValue';
}
var something = getSomething(); // something = someValue

// Operate on something else. No explicit return value
function changeSomething(something) {
  something = 'someOtherValue';
};
changeSomething(something); // something = someOtherValue

// Setting the variable to the return of a non-explicit return value
something = changeSomething(something); // something = undefined

// Return this to chain
var hash = {
  addProp: function (name, value) {
    this[name] = value;
    return this;
  }
};

hash.addProp('test', 'value').addProp('test2', 'value2'); // hash = Object {addProp: function, test: "value", test2: "value2"}
于 2013-06-05T23:07:58.113 回答