javascript函数替换不起作用,为什么?

javascript函数替换不起作用,为什么?,javascript,replace,Javascript,Replace,为什么javascript函数替换不起作用 social_share: function(e){ var is_video, social_name = $(e).attr('class'), share_url = document.URL, share_title = $('title').text(), share_media, share;

为什么javascript函数替换不起作用

    social_share: function(e){

        var is_video,
            social_name = $(e).attr('class'),
            share_url = document.URL,
            share_title = $('title').text(),
            share_media,
            share;

        switch(social_name) {

            case 'twitter':
                share = 'https://twitter.com/share?url={share_url}&text={share_title}';
            break;

            case 'facebook':
                share = 'https://www.facebook.com/dialog/feed?app_id=123050457758183&link={share_url}&picture={share_media}&name={share_title}';
            break;

            case 'google':
                share = 'https://plus.google.com/share?url={share_url}';
            break;

            case 'pinterest':
                share = 'https://pinterest.com/pin/create/bookmarklet/?media={share_media}&url={share_url}&is_video={is_video}&description={share_title}';
            break;

            case 'mailto':
                share = '...';
            break;

        }

        share.replace('{share_title}', share_title)
             .replace('{share_url}', encodeURI(share_url))
             .replace('{share_media}', encodeURI(share_media))
             .replace('{is_video}', is_video);

        console.log(share);

    },
和console.log函数返回字符串共享,不替换。。。 它将通过{share_url}&text={share_title}如果twitter和其他人的

将返回新值,因此您需要将其保存到变量:

share = share.replace( ... )
将返回新值,因此需要将其保存到变量:

share = share.replace( ... )

字符串在javascript中是不可变的。因此,通过任何字符串函数更改字符串将返回一个新字符串作为输出。在上述代码中,将语法更改为:

share = share.replace('{share_title}', share_title);

字符串在javascript中是不可变的。因此,通过任何字符串函数更改字符串将返回一个新字符串作为输出。在上述代码中,将语法更改为:

share = share.replace('{share_title}', share_title);

encodeURI函数怎么样?它对社交共享的URL很有用?出于兴趣,您首先是如何知道
replace
函数的?只是想知道你看到的哪些源代码没有告诉你正确的使用方法是我的错误,我没有看到它,需要休息一下…函数encodeURI怎么样,它对社交共享的URL很好?出于兴趣,你一开始是怎么知道
replace
函数的?只是想知道你看到了什么来源没有告诉你正确的使用方法是我的错误,我没有看到,需要休息一下。。。