Home > Web Front-end > JS Tutorial > body text

Example of custom format formatted output in js

小云云
Release: 2018-03-10 15:28:22
Original
2106 people have browsed it

在使用js的时候,经常要进行字符串的拼接,一但使用+号进行字符串拼接的时候,基本是各种问题,又不好维护,有没有更好的方法地其进行格式化输出呢?答案肯定是有的,如果你使用nodejs,它已经自带的,如果你还在使用纯原生js,那不好意思了。

使用方法

String对象添加format方法

String.prototype.format = function(opts) {//use 'my name is ${name}'.format({name:'lake'})
    var data = Array.prototype.slice.call(arguments, 0),
        toString = Object.prototype.toString;    if (data.length) {
        data = data.length == 1 ?
            (opts !== null && (/\[object Array\]|\[object Object\]/.test(toString.call(opts))) ? opts : data) : data;        return this.replace(/\$\{(.+?)\}/g, function(match, key) {
            var replacer = data[key];            // chrome 下 typeof /a/ == 'function'

            if ('[object Function]' == toString.call(replacer)) {
                replacer = replacer(key);
            }            return ('undefined' == typeof replacer ? '' : replacer);
        });
    }    return this;
}
Copy after login
Copy after login

使用方法

console.log('my name is ${name}.'.format({name:'lake'}))
Copy after login
Copy after login
  • 1

输出结果

my name is lake.
Copy after login

权声明:多一份转载,多一份环保 http://blog.csdn.net/dounine/article/details/78443487

在使用js的时候,经常要进行字符串的拼接,一但使用+号进行字符串拼接的时候,基本是各种问题,又不好维护,有没有更好的方法地其进行格式化输出呢?答案肯定是有的,如果你使用nodejs,它已经自带的,如果你还在使用纯原生js,那不好意思了。

使用方法

String对象添加format方法

String.prototype.format = function(opts) {//use 'my name is ${name}'.format({name:'lake'})
    var data = Array.prototype.slice.call(arguments, 0),
        toString = Object.prototype.toString;    if (data.length) {
        data = data.length == 1 ?
            (opts !== null && (/\[object Array\]|\[object Object\]/.test(toString.call(opts))) ? opts : data) : data;        return this.replace(/\$\{(.+?)\}/g, function(match, key) {
            var replacer = data[key];            // chrome 下 typeof /a/ == 'function'

            if ('[object Function]' == toString.call(replacer)) {
                replacer = replacer(key);
            }            return ('undefined' == typeof replacer ? '' : replacer);
        });
    }    return this;
}
Copy after login
Copy after login

使用方法

console.log('my name is ${name}.'.format({name:'lake'}))
Copy after login
Copy after login

输出结果

my name is lake.
Copy after login

相关推荐:

php阅读number_format函数的源码分享

推荐10篇关于format函数的课程

详解str.format()的基本语法和高级用法

The above is the detailed content of Example of custom format formatted output in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template