JS基本应用-函数

  • 函数声明与函数表达式、对象实例化的区别
  1. add1(1,1);
  2. add2(1,2);
  3. add3(1,3);
  4. function add1(i, j){
  5. console.log(i+j);
  6. }
  7. var add2 = function(i, j){
  8. console.log(i+j);
  9. }
  10. var add3 = new Function("i", "j", "console.log(i+j);");
  • 对象实例化与函数声明与函数表达式的区别
  1. (function(){
  2. var i = 10;
  3. function add(j) {
  4. console.log(i+j);
  5. debugger;
  6. }
  7. add(1);
  8. })();
  9. (function(){
  10. var i = 10;
  11. var add = new Function("j", "console.log(i+j);debugger;");
  12. add(1);
  13. })();
  • bind的使用
  1. function Point(x, y){
  2. this.x = x;
  3. this.y = y;
  4. }
  5. Point.prototype.move = function(x, y) {
  6. this.x += x;
  7. this.y += y;
  8. }
  9. var p = new Point(0,0);
  10. var circle = {x:1, y:1, r:1};
  11. var circleMove = p.move.bind(circle, 2, 1);
  12. circleMove();
  • 构造函数
  1. function Car(type,color){
  2. this.type = type;
  3. this.color = color;
  4. this.status = "stop";
  5. this.light = "off";
  6. }
  7. Car.prototype.start = function(){
  8. this.status = "driving";
  9. this.light = "on";
  10. console.log(this.type + " is " + this.status);
  11. }
  12. Car.prototype.stop = function(){
  13. this.status = "stop";
  14. this.light = "off";
  15. console.log(this.type + " is " + this.status);
  16. }
  17. var audi = new Car("audi", "silver");
  18. var benz = new Car("benz", "black");
  19. var ferrari = new Car("ferrari", "yellow");
  • 函数调用模式
  1. function add(i, j){
  2. return i+j;
  3. }
  4. var myNumber = {
  5. value: 1,
  6. double: function(){
  7. var helper = function(){
  8. this.value = add(this.value,this.value);
  9. }
  10. helper();
  11. }
  12. }
  • arguments转数组
  1. function add(i, j) {
  2. var args = Array.prototype.slice.apply(arguments);
  3. args.forEach(function(item){
  4. console.log(item);
  5. })
  6. }
  7. add(1,2,3);
  • arguments.callee使用
  1. console.log(
  2. (function(i){
  3. if (i==0) {
  4. return 1;
  5. }
  6. return i * arguments.callee(i-1);
  7. })(5)
  8. );
  • 递归
  1. function factorial(i){
  2. if (i==0) {
  3. return 1;
  4. }
  5. return i*factorial(i-1);
  6. }
  • 普通递归函数跟记忆函数调用次数对比
  1. var factorial = (function(){
  2. var count = 0;
  3. var fac = function(i){
  4. count++;
  5. if (i==0) {
  6. console.log('调用次数:' + count);
  7. return 1;
  8. }
  9. return i*factorial(i-1);
  10. }
  11. return fac;
  12. })();
  13. for(var i=0;i<=10;i++){
  14. console.log(factorial(i));
  15. }
  16. //记忆函数
  17. var factorial = (function(){
  18. var memo = [1];
  19. var count = 0;
  20. var fac = function(i){
  21. count++;
  22. var result = memo[i];
  23. if(typeof result === 'number'){
  24. console.log('调用次数:' + count);
  25. return result;
  26. }
  27. result = i*fac(i-1);
  28. memo[i] = result;
  29. return result;
  30. }
  31. return fac;
  32. })();
  33. for(var i=0;i<=10;i++){
  34. console.log(factorial(i));
  35. }
  • curry 函数柯里化
    • 把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术
  1. function add(value){
  2. var helper = function(next){
  3. value = typeof(value)==="undefined"?next:value+next;
  4. return helper;
  5. }
  6. helper.valueOf = function(){
  7. return value;
  8. }
  9. return helper
  10. }
  • 定时器
  1. function set_loop() {
  2. var i =1;
  3. setTimeout(function () {
  4. i = i + 1;
  5. console.log(i);
  6. set_loop()
  7. },5000);
  8. }