//回调函数实现的原理,当把一个函数当做一个参数传进另一个函数的时候只是传了这个函数的定义并没有执行
/*$('#btn').click(function(){
alert('回调函数')
})*/
//callback函数的实现原理 !没有this指向的时候
/*var allUserData = [];
function logstuff(userData){
if(typeof userData =='string'){
console.log(userData)
}else if(typeof userData =='object'){
for(item in userData){
console.log(item+":"+userData[item])
}
}
}
function getIpt(opt,callback){
allUserData.push(opt)
console.log(typeof callback)
if(typeof callback ==='function'){
callback(opt)
}
}
var friends = {name:"Rich", speciality:"JavaScript"}
getIpt(friends,logstuff)*/
//从在this指向的时候
/*var allUserData = [];
var fullName = '';
var logstuff = {
id:'24242',
fullName:'no set',
setName:function(){
console.log(this)
this.fullName = '我设置过了'
},
}
function getIpt(opt,callback,callObj){
allUserData.push(opt)
console.log(typeof callback)
if(typeof callback =='function'){
callback.call(callObj,opt)//call 的第一个参数是指定上下文对象,后面的饿值的是参数,在这里为了保护this的指向,不然指向window
}
}
var friends = {name:"Rich", speciality:"JavaScript"}
getIpt(friends,logstuff.setName,logstuff)
console.log(logstuff.fullName)
console.log(fullName)*/
//再具体的项目当中使用 需要把名字组合起来之后在在判断是男女,就先掉公共的方法然后在执行各自的函数在判断男女
/*function genericPoemMaker(name, gender) {
console.log(name + " is finer than fine wine.");
console.log("Altruistic and noble for the modern time.");
console.log("Always admirably adorned with the latest style.");
console.log("A " + gender + " of unfortunate tragedies who still manages a perpetual smile");
}
function getUserInput(firstName, lastName, gender, callback) {
var fullName = firstName + " " + lastName;
if(typeof callback === "function") {
callback(fullName, gender);
}
}
getUserInput("Michael", "Fassbender", "Man", genericPoemMaker);
function greetUser(customerName, sex) {
var salutation = sex && sex === "Man" ? "Mr." : "Ms.";
console.log("Hello, " + salutation + " " + customerName);
}
getUserInput("Bill", "Gates", "Man", greetUser);*/
//在nodejs中常用的
/*aap.use(function(req,res,next){
var err = new Error('Not Fond');
err.statue = 404;
next(err)
})*/
//具体实现的原理
var doSomeThing = function(value,callback){
console.log(callback)
var a=1,
b=2,
c=3;
return callback(a,b,c)
}
var sum = function(a,b,c){
return a+b+c
}
var d = doSomeThing(1,sum)
// var d = doSomeThing(1,function(a,b,c){
// return a+b+c
// })
console.log(d)