if( [] == false ) //true
if( {} == false ) //false
if( {} ) //true
if( [] ) //true
function foo(){
this.name = '小明';
console.log(this.name);
}
foo(); //输出小明
window.name //小明
url转置,www.toutiao.com => com.toutiao.www
var url = 'www.toutiao.com';
var array = url.split("");
array = array.reverse();
url = array.join("");
split() 方法用于把一个字符串分割成字符串数组。
string.split(separator,howmany)
- separator 必需。字符串或正则表达式,从该参数指定的地方分割 stringObject。
如果把空字符串 (“”) 用作 separator,那么 stringObject 中的每个字符之间都会被分割。 - howmany 可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
- separator 必需。字符串或正则表达式,从该参数指定的地方分割 stringObject。
join() 方法用于把数组中的所有元素放入一个字符串。
array.join(separator)
- separator 可选。指定要使用的分隔符。如果省略该参数,则使用逗号作为分隔符。
写一个搜索框,右侧搜索按钮固定宽度,左侧自适应
bind函数实现
bind() 函数会创建一个新函数(称为绑定函数),新函数与被调函数(绑定函数的目标函数)具有相同的函数体(在 ECMAScript 5 规范中内置的call属性)。当新函数被调用时 this 值绑定到 bind() 的第一个参数,该参数不能被重写。绑定函数被调用时,bind() 也接受预设的参数提供给原函数。一个绑定函数也能使用new操作符创建对象:这种行为就像把原函数当成构造器。提供的 this 值被忽略,同时调用时的参数被提供给模拟函数。
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
// 获取调用时(fBound)的传参.bind 返回的函数入参往往是这么传递的
aArgs.concat(Array.prototype.slice.call(arguments)));
};
// 维护原型关系
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
}