watch: {
'bindForm.name'(val) {
let pattern = /[^\u4e00-\u9fa5]+/
this.bindForm.name = val.replace(pattern, '')
if (val.length > 32) {
this.bindForm.name = val.substring(0, 32)
}
this.checkIsComplete()
},
'bindForm.phone'(val) {
if (val.length > 11) {
this.bindForm.phone = val.substring(0, 11)
}
this.checkIsComplete()
},
'bindForm.code'(val) {
if (val.length > 6) {
this.bindForm.code = val.substring(0, 6)
}
this.checkIsComplete()
},
}
function getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
var reg_rewrite = new RegExp('(^|/)' + name + '/([^/]*)(/|$)', 'i')
var r = window.location.search.substr(1).match(reg)
var q = window.location.pathname.substr(1).match(reg_rewrite)
if (r != null) {
return unescape(r[2])
} else if (q != null) {
return unescape(q[2])
} else {
return null
}
}
var obj1 = {};
console.log( Object.getPrototypeOf( obj1 ) === Object.prototype ); // 输出:true
var obj = {
myName: 'sven',
getName: function(){
return this.myName;
}
};
console.log( obj.getName() ); // 'sven'
var getName2 = obj.getName;
console.log( getName2() ); // undefined
构造函数显式地返回一个对象
var MyClass = function(){
this.name = 'sven';
return { // 显式地返回一个对象
name: 'anne'
}
}
var obj = new MyClass();
alert ( obj.name ); // 输出:anne
函数内部函数func内的this指向window
document.getElementById('div1').onclick = function(){
alert(this.id); // this指向div,输出:div1
var func = function(){
alert(this.id); // this指向window,输出:undefined
}
func();
}
应用于数组运算
Math.max.apply( null, [ 1, 2, 5, 3, 4 ] ) // 输出:5
var Type = {};
for(var i = 0, type; type = ['String', 'Array', 'Number'][i++];){
(function(type){
Type['is' + type] = function(obj){
return Object.prototype.toString.call(obj) === '[object ' + type + ']';
}
})(type);
}
Type.isArray([]);
Type.isString('str');
(function(){
Array.protorype.push.call(argument, 4); // argments借用Array.protorype.push方法
console.log(argments); // 输出:[1, 2, 3, 4]
})(1, 2, 3)
var throttle = function(fn, interval){
var _self = fn, // 保存需要被延迟执行的函数引用
timer, // 定时器
firstTime = true; // 是否是第一次调用
return function() {
var args = arguments,
_me = this;
if(firstTime) {
_self.apply(_me, args);
return firstTime = false;
}
if(timer) { // 如果定时器还在,说明前一次延迟执行还没有完成
return false;
}
timer = setTimeout(function() {
clearTimeout(timer);
timer = null;
_self.apply(_me, args);
}, interval || 500);
}
}
// 使用
window.onresize = throttle(function() {
console.log(1);
}, 500)
function flatten(){
var ret=[];
return function flat(a){
for(var item of a){
if(item.constructor===Array){
ret.concat(flat(item))
}else{
ret.push(item)
}
}
return ret
}
}
arr.toString().split(",").map(Number)
// ES6
arr.flat(dep)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。