为了账号安全,请及时绑定邮箱和手机立即绑定

javascript: 一些经典的小代码

标签:
JavaScript
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>demo</title>
</head>
<body>
<script type="text/javascript">
// //1.函数声明与函数表达式、对象实例化的区别
// add1(1,1);
// add2(1,2);
// add3(1,3);
// function add1(i, j){
//   console.log(i+j);
// }
// var add2 = function(i, j){
//   console.log(i+j);
// }
// var add3 = new Function("i", "j", "console.log(i+j);");

//2.对象实例化与函数声明与函数表达式的区别
// (function(){
//   var i = 10;
//   function add(j) {
//     console.log(i+j);
//     debugger;
//   }
//   add(1);
// })();
// (function(){
//   var i = 10;
//   var add = new Function("j", "console.log(i+j);debugger;");
//   add(1);
// })();

// // bind的使用
// function Point(x, y){
//     this.x = x;
//     this.y = y;
// }
// Point.prototype.move = function(x, y) {
//     this.x += x;
//     this.y += y;
// }
// var p = new Point(0,0);
// var circle = {x:1, y:1, r:1};
// var circleMove = p.move.bind(circle, 2, 1);
// circleMove();

// // 构造函数
// function Car(type,color){
//   this.type = type;
//   this.color = color;
//   this.status = "stop";
//   this.light = "off";
// }
// Car.prototype.start = function(){
//   this.status = "driving";
//   this.light = "on";
//   console.log(this.type + " is " + this.status);
// }
// Car.prototype.stop = function(){
//   this.status = "stop";
//   this.light = "off";
//   console.log(this.type + " is " + this.status);
// }
// var audi = new Car("audi", "silver");
// var benz = new Car("benz", "black");
// var ferrari = new Car("ferrari", "yellow");

// // 函数调用模式
// function add(i, j){
//   return i+j;
// }
// var myNumber = {
//   value: 1,
//   double: function(){
//     var helper = function(){
//       this.value = add(this.value,this.value);
//     }
//     helper();
//   }
// }

// // arguments转数组
// function add(i, j) {
//   var args = Array.prototype.slice.apply(arguments);
//   args.forEach(function(item){
//     console.log(item);
//   })
// }
// add(1,2,3);

// // arguments.callee使用
// console.log(
//   (function(i){
//     if (i==0) {
//       return 1;
//     }
//     return i*arguments.callee(i-1);
//   })(5)
// );

// // 递归
// function factorial(i){
//   if (i==0) {
//     return 1;
//   }
//   return i*factorial(i-1);
// }

// // 普通递归函数跟记忆函数调用次数对比
// var factorial = (function(){
//   var count = 0;
//   var fac = function(i){
//     count++;
//     if (i==0) {
//       console.log('调用次数:' + count); 
//       return 1;
//     }
//     return i*factorial(i-1);
//   }
//   return fac;
// })();
// for(var i=0;i<=10;i++){
//   console.log(factorial(i)); 
// }

// // 记忆函数
// var factorial = (function(){
//   var memo = [1];
//   var count = 0;
//   var fac = function(i){
//     count++;
//     var result = memo[i];
//     if(typeof result === 'number'){
//       console.log('调用次数:' + count); 
//       return result;
//     } 
//     result = i*fac(i-1);
//     memo[i] = result;
//     return result;
//   }
//   return fac;
// })();
// for(var i=0;i<=10;i++){
//   console.log(factorial(i)); 
// }

// // curry函数柯里化
// function add(value){
//    var helper = function(next){
//       value = typeof(value)==="undefined"?next:value+next;
//       return helper;
//    }
//    helper.valueOf = function(){
//      return value;
//    }
//    return helper
// }

</script>
</body>
</html>
点击查看更多内容
3人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消