//输入字符串返回字符串
function spacify(str){
return str.split('').join('');
}
console.log(spacify('hello world'));
// 'hello world'.spacify();
//函数直接作用在一个字符串对象上(原型连)
String.prototype.spacify2 = function(){
return this.split('').join('');
}
console.log('hello boy'.spacify2())
//函数声明和函数表达式的区别---待续
//控制台输出方式
function log(msg){
console.log(msg);
}
log('hello boy2'.spacify2())
//apply的运用 arguments是伪数组
function log2(){
console.log.apply(console,arguments);
}
//输出的字符串统一加上(app)这样的字符串类似于:'(app) hello world'
function log3(){
var args = Array.prototype.slice.call(arguments);
args.unshift('(app)');
console.log.apply(console,args);
}
log3('hello3')
//this的理解
var User = {
conunt:1,
getConunt:function(){
return this.conunt;
}
}
console.log(User.getConunt());//1
var func = User.getConunt;
console.log(func());//undefind 因为func的上下文是'window'
//正确的是使用bind
var func2 = User.getConunt.bind(User);
console.log(func2())
//兼容低浏览器
Function.prototype.bind = Function.prototype.bind || function(context){
var self =this;
return function(){
return.self.apply(context,arguments);
}
}