Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

看了你不少文章,路过写写 #4

Open
noneven opened this issue Mar 11, 2016 · 2 comments
Open

看了你不少文章,路过写写 #4

noneven opened this issue Mar 11, 2016 · 2 comments

Comments

@noneven
Copy link

noneven commented Mar 11, 2016

var jsonP = (function(){

    var root = this;

    var param2query = function(data){
        var query = "?";
        for(var k in data){
            if(!data.hasOwnProperty(k)) break;
            query+= (k)+"="+(data[k])+"&";
        }
        return query;
    };

    return function(url, option){

        var data, callback;

        if ("[object Object]" == Object.prototype.toString.call(url)) {
            url = url.url;
            data = url.data;
            callback = url.callback;
        }else{
            data = option.data;
            callback = option.callback;
        }

        var jsonPCallbackName = "jsonP" + new Date().getTime();

        url = url.replace(/\?$/,"") + param2query(data);

        url += "callback=" + jsonPCallbackName;

        var script = document.createElement("script");
        script.src = url;

        root[jsonPCallbackName] = function(data){
            callback.call(jsonP, data)
            delete root[jsonPCallbackName];
            document.body.removeChild(script)
        }

        document.body.appendChild(script);
    }
}).call(this);

jsonP("https://passport.58.com/wukong/userstate", {
    data: {
        user: "cat",
        pword: "any"
    },
    callback: function(data) {
        console.log(data)
    }
})

少个umd封装

@island205
Copy link
Owner

if(!data.hasOwnProperty(k)) break; 为什么要 break

@noneven
Copy link
Author

noneven commented Mar 11, 2016

我不太喜欢

if(true){
    //todo
    if(true){
        //todo
    }
}
//习惯这样
if (!false) return 
if (!false) break
if (!false) continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants