JavaScript

JavaScript---让编程更加有趣

引入js,无论什么编程语言,基础都很重要.才能了解本质,

   javascript 简称JS,是一门编程语言,有名的脚本语言,本身跟Java并没有关系,它可以网页增光添彩,提高我们访问网页可视化效果!

   HTML,CSS,JS组成前端结构,三者相辅相成,如果说HTML是外层骨骼,CSS是肉体,那我认为JS就是其中的灵魂.

ECMAScript基础语法

1.js的引入方式?

  行内式

  内接式

  外接式

2.变量

js变量需要声明,var  跟python有些区别  定义一个变量需要先声明

示例1 
var a='123'
var b=demo

示例2
var a = 12,b = 5;
    a = a/b;
    console.log(a);  /*  结果为2.4*/

示例3
    var x= 5;
    var y =x++;
    console.log(x);   //x=6
    console.log(y);   //y=5
    x++
    x+=1  先赋值(赋值给y) 后++ 给x

示例4
    var a = 5;
    var b= '5';
    console.log(a==b);     //  true 比较的是值
    console.log(a===b);   // false  比较的是值和数据类型

示例5 
var name = 'Sheldon';
var age = 29;
var hobby = 'crazy';
//方式1
var str = name + '现已'+ age + '岁了,还是那么'+ hobby;
console.log(str);
//方式2 es6模板字符串``,如果有变量使用$(变量名)
var str2 = `${name}现已${age}岁了,还是那么${hobby}`;
console.log(str2);
结果都是 ''Sheldon现已29岁了,还是那么crazy''


//通过以上示例相信你对var变量语法了解颇深了吧
var变量示例

3. 数据类型

  js分为基础数据类型和引用数据类型

基础数据类型

  • number  --数值
    • var a = 10;
      //typeof  检查什么数据类型,类似python中type
      console.log(typeof a)
      //number
      
      //特殊示例
      var a=10;b=0;
      var c=a/b
      console.log(typeof c)
      // Infinity   无限大.  number
      number示例
  • string  ---字符串
    • var a='666'
      console.log(typeof a)
      // string
      string示例
  • boolean  ---布尔值
    • 1 var x=false
      2 console.log(typeof x)
      3 //boolean  
      boolean示例
  • null      ---空的意思
    • 1 var y=null    //空对象.object
      2 console.log(y)
      3 //null
      null示例
  • undefined   ---没有被定义
var z;
console.log(typeof z);
// undefined  只声明,没有定义
undefined示例

引用数据类型

  • Function
  • Object
  • Arrary
  • Date

4. 数据类型转换

数值转字符串

  1. 隐式转换 数值+''
  2. String() 强转
  3. 数值 .toString()
var a = 5;
var astr = String(a);
console.log(typeof astr); 
console.log(typeof a.toString());

 // 结果都为字符串string
数值转字符串

 

字符串转数值

  1. Number() 强转
  2. parseInt() 转整型,有小数点只保留整数部分
  3. parseFloat() 转小数
1 var s_n = '1.234433333333333333335';
2 console.log(Number(s_n)); //1.2344333333333333
3 console.log(typeof Number(s_n)); //number
4 
5 console.log(parseInt(s_n));  // 1
6 console.log(parseFloat(s_n)); // 1.2344333333333333
字符串转数值

 

任何类型转Boolean

var a1 = '123';
console.log(Boolean(a1));   //true
var a2 = -123;
console.log(Boolean(a2)); //true
var a3 = Infinity;
console.log(Boolean(a5)); //true

var a4 = 0;
console.log(Boolean(a3)); //false
var a5 = null;
console.log(Boolean(a4)); //false
var a6 = NaN;
var a7 = undefined;
console.log(Boolean(a6)); //false
console.log(Boolean(a7)); //false

//由此只要记住 0 null NaN undefined 转boolean都是false,其他都为true
其他类型转boolean

 

5.测试语句

console.log    

  类似于python中的print

window.alert()   

   显示一个警告对话框,上面显示指定文本内容,以及确定按钮,  window 是可以省略不写

confirm('确定删除吗?')     

  显示一盒带有指定文本内容以及确认以及取消按钮的对话框,如果访问者点击''确认''返回true,否则返回false

6.流程控制

    if-else

var age = 15;
if(age>18){
    console.log('h1');
}else if(age<18 && age>10){
    console.log('div');
}else{

}
console.log('p');
// 输出:
//     div
//     p
// if else 运用上与C语言有着异曲同工之妙
if-else

 

 switch  开关

 1 //switch开关  表示不同的条件执行不同的动作
 2 //case表示一个条件,满足这个条件就会走进来,break跳出,如果不写break,就会走进下一个程序直至遇到break结束.
 3 var danceScore = '68';
 4 switch (danceScore) {
 5     case '90':
 6         console.log('perfect')
 7         break; //退出
 8     case 80:
 9         console.log('best')
10         break;
11     case 70:
12         console.log('good')
13         break;
14     default:
15         console.log('bad')
16 }
switch

 

while 循环    ---1.初始化循环变量  2.判断循环条件  3.更新循环变量

 1 示例1打印1-10
 2 var i =1;
 3 while(i<11){
 4     console.log(i);
 5     i++;
 6     //i +=1
 7 }
 8 示例2打印0-100内偶数
 9 var a = 0;
10 while (a<=100){
11     if (a % 2===0){
12         console.log(a);
13     }
14     a++;
15 }
while循环

 

do while 循环  //  while循环中先执行初始化循环变量再执行循环

var i=3;  
do {
    console.log(i);
    i++; 
}while (i<10) 
//输出3,-9
do while

 

for循环以及for循环嵌套

示例1打印0-100
 for(var i=0;i<100;i++){
     console.log(i);
}

示例2//双重for循环,打印长方形
for(var i=0;i<6;i++){ //控制行数
    for(var j=0;j<10;j++){ //每行*数
        document.write('*')
    }
    document.write('<br>')
}

示例3打印三角形
for(var i=1;i<7;i++){
    for (var s=i; s<7;s++){
        document.write('&nbsp;')
    }
    for(var j=1;j<2*(i-1);j++){
        document.write('*')
    }
    document.write('<br>')
}

示例4打印直角三角形

for(var i=0;i<7;i++){
    for (var j=1; j<=i;j++){
        document.write('*')
    }
    document.write('<br/>')
}
for循环

7. DOM事件

  三大步骤:  1. 获取事件对象

        2. 事件

        3. 事件驱动/处理,业务逻辑

 

 

 

  

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

posted @ 2019-01-04 17:59  FindSoul  阅读(190)  评论(0)    收藏  举报
var RENDERER = { POINT_INTERVAL : 5, FISH_COUNT : 3, MAX_INTERVAL_COUNT : 50, INIT_HEIGHT_RATE : 0.5, THRESHOLD : 50, init : function(){ this.setParameters(); this.reconstructMethods(); this.setup(); this.bindEvent(); this.render(); }, setParameters : function(){ this.$window = $(window); this.$container = $('#jsi-flying-fish-container'); this.$canvas = $(''); this.context = this.$canvas.appendTo(this.$container).get(0).getContext('2d'); this.points = []; this.fishes = []; this.watchIds = []; }, createSurfacePoints : function(){ var count = Math.round(this.width / this.POINT_INTERVAL); this.pointInterval = this.width / (count - 1); this.points.push(new SURFACE_POINT(this, 0)); for(var i = 1; i < count; i++){ var point = new SURFACE_POINT(this, i * this.pointInterval), previous = this.points[i - 1]; point.setPreviousPoint(previous); previous.setNextPoint(point); this.points.push(point); } }, reconstructMethods : function(){ this.watchWindowSize = this.watchWindowSize.bind(this); this.jdugeToStopResize = this.jdugeToStopResize.bind(this); this.startEpicenter = this.startEpicenter.bind(this); this.moveEpicenter = this.moveEpicenter.bind(this); this.reverseVertical = this.reverseVertical.bind(this); this.render = this.render.bind(this); }, setup : function(){ this.points.length = 0; this.fishes.length = 0; this.watchIds.length = 0; this.intervalCount = this.MAX_INTERVAL_COUNT; this.width = this.$container.width(); this.height = this.$container.height(); this.fishCount = this.FISH_COUNT * this.width / 500 * this.height / 500; this.$canvas.attr({width : this.width, height : this.height}); this.reverse = false; this.fishes.push(new FISH(this)); this.createSurfacePoints(); }, watchWindowSize : function(){ this.clearTimer(); this.tmpWidth = this.$window.width(); this.tmpHeight = this.$window.height(); this.watchIds.push(setTimeout(this.jdugeToStopResize, this.WATCH_INTERVAL)); }, clearTimer : function(){ while(this.watchIds.length > 0){ clearTimeout(this.watchIds.pop()); } }, jdugeToStopResize : function(){ var width = this.$window.width(), height = this.$window.height(), stopped = (width == this.tmpWidth && height == this.tmpHeight); this.tmpWidth = width; this.tmpHeight = height; if(stopped){ this.setup(); } }, bindEvent : function(){ this.$window.on('resize', this.watchWindowSize); this.$container.on('mouseenter', this.startEpicenter); this.$container.on('mousemove', this.moveEpicenter); this.$container.on('click', this.reverseVertical); }, getAxis : function(event){ var offset = this.$container.offset(); return { x : event.clientX - offset.left + this.$window.scrollLeft(), y : event.clientY - offset.top + this.$window.scrollTop() }; }, startEpicenter : function(event){ this.axis = this.getAxis(event); }, moveEpicenter : function(event){ var axis = this.getAxis(event); if(!this.axis){ this.axis = axis; } this.generateEpicenter(axis.x, axis.y, axis.y - this.axis.y); this.axis = axis; }, generateEpicenter : function(x, y, velocity){ if(y < this.height / 2 - this.THRESHOLD || y > this.height / 2 + this.THRESHOLD){ return; } var index = Math.round(x / this.pointInterval); if(index < 0 || index >= this.points.length){ return; } this.points[index].interfere(y, velocity); }, reverseVertical : function(){ this.reverse = !this.reverse; for(var i = 0, count = this.fishes.length; i < count; i++){ this.fishes[i].reverseVertical(); } }, controlStatus : function(){ for(var i = 0, count = this.points.length; i < count; i++){ this.points[i].updateSelf(); } for(var i = 0, count = this.points.length; i < count; i++){ this.points[i].updateNeighbors(); } if(this.fishes.length < this.fishCount){ if(--this.intervalCount == 0){ this.intervalCount = this.MAX_INTERVAL_COUNT; this.fishes.push(new FISH(this)); } } }, render : function(){ requestAnimationFrame(this.render); this.controlStatus(); this.context.clearRect(0, 0, this.width, this.height); this.context.fillStyle = 'hsl(0, 0%, 95%)'; for(var i = 0, count = this.fishes.length; i < count; i++){ this.fishes[i].render(this.context); } this.context.save(); this.context.globalCompositeOperation = 'xor'; this.context.beginPath(); this.context.moveTo(0, this.reverse ? 0 : this.height); for(var i = 0, count = this.points.length; i < count; i++){ this.points[i].render(this.context); } this.context.lineTo(this.width, this.reverse ? 0 : this.height); this.context.closePath(); this.context.fill(); this.context.restore(); } }; var SURFACE_POINT = function(renderer, x){ this.renderer = renderer; this.x = x; this.init(); }; SURFACE_POINT.prototype = { SPRING_CONSTANT : 0.03, SPRING_FRICTION : 0.9, WAVE_SPREAD : 0.3, ACCELARATION_RATE : 0.01, init : function(){ this.initHeight = this.renderer.height * this.renderer.INIT_HEIGHT_RATE; this.height = this.initHeight; this.fy = 0; this.force = {previous : 0, next : 0}; }, setPreviousPoint : function(previous){ this.previous = previous; }, setNextPoint : function(next){ this.next = next; }, interfere : function(y, velocity){ this.fy = this.renderer.height * this.ACCELARATION_RATE * ((this.renderer.height - this.height - y) >= 0 ? -1 : 1) * Math.abs(velocity); }, updateSelf : function(){ this.fy += this.SPRING_CONSTANT * (this.initHeight - this.height); this.fy *= this.SPRING_FRICTION; this.height += this.fy; }, updateNeighbors : function(){ if(this.previous){ this.force.previous = this.WAVE_SPREAD * (this.height - this.previous.height); } if(this.next){ this.force.next = this.WAVE_SPREAD * (this.height - this.next.height); } }, render : function(context){ if(this.previous){ this.previous.height += this.force.previous; this.previous.fy += this.force.previous; } if(this.next){ this.next.height += this.force.next; this.next.fy += this.force.next; } context.lineTo(this.x, this.renderer.height - this.height); } }; var FISH = function(renderer){ this.renderer = renderer; this.init(); }; FISH.prototype = { GRAVITY : 0.4, init : function(){ this.direction = Math.random() < 0.5; this.x = this.direction ? (this.renderer.width + this.renderer.THRESHOLD) : -this.renderer.THRESHOLD; this.previousY = this.y; this.vx = this.getRandomValue(4, 10) * (this.direction ? -1 : 1); if(this.renderer.reverse){ this.y = this.getRandomValue(this.renderer.height * 1 / 10, this.renderer.height * 4 / 10); this.vy = this.getRandomValue(2, 5); this.ay = this.getRandomValue(0.05, 0.2); }else{ this.y = this.getRandomValue(this.renderer.height * 6 / 10, this.renderer.height * 9 / 10); this.vy = this.getRandomValue(-5, -2); this.ay = this.getRandomValue(-0.2, -0.05); } this.isOut = false; this.theta = 0; this.phi = 0; }, getRandomValue : function(min, max){ return min + (max - min) * Math.random(); }, reverseVertical : function(){ this.isOut = !this.isOut; this.ay *= -1; }, controlStatus : function(context){ this.previousY = this.y; this.x += this.vx; this.y += this.vy; this.vy += this.ay; if(this.renderer.reverse){ if(this.y > this.renderer.height * this.renderer.INIT_HEIGHT_RATE){ this.vy -= this.GRAVITY; this.isOut = true; }else{ if(this.isOut){ this.ay = this.getRandomValue(0.05, 0.2); } this.isOut = false; } }else{ if(this.y < this.renderer.height * this.renderer.INIT_HEIGHT_RATE){ this.vy += this.GRAVITY; this.isOut = true; }else{ if(this.isOut){ this.ay = this.getRandomValue(-0.2, -0.05); } this.isOut = false; } } if(!this.isOut){ this.theta += Math.PI / 20; this.theta %= Math.PI * 2; this.phi += Math.PI / 30; this.phi %= Math.PI * 2; } this.renderer.generateEpicenter(this.x + (this.direction ? -1 : 1) * this.renderer.THRESHOLD, this.y, this.y - this.previousY); if(this.vx > 0 && this.x > this.renderer.width + this.renderer.THRESHOLD || this.vx < 0 && this.x < -this.renderer.THRESHOLD){ this.init(); } }, render : function(context){ context.save(); context.translate(this.x, this.y); context.rotate(Math.PI + Math.atan2(this.vy, this.vx)); context.scale(1, this.direction ? 1 : -1); context.beginPath(); context.moveTo(-30, 0); context.bezierCurveTo(-20, 15, 15, 10, 40, 0); context.bezierCurveTo(15, -10, -20, -15, -30, 0); context.fill(); context.save(); context.translate(40, 0); context.scale(0.9 + 0.2 * Math.sin(this.theta), 1); context.beginPath(); context.moveTo(0, 0); context.quadraticCurveTo(5, 10, 20, 8); context.quadraticCurveTo(12, 5, 10, 0); context.quadraticCurveTo(12, -5, 20, -8); context.quadraticCurveTo(5, -10, 0, 0); context.fill(); context.restore(); context.save(); context.translate(-3, 0); context.rotate((Math.PI / 3 + Math.PI / 10 * Math.sin(this.phi)) * (this.renderer.reverse ? -1 : 1)); context.beginPath(); if(this.renderer.reverse){ context.moveTo(5, 0); context.bezierCurveTo(10, 10, 10, 30, 0, 40); context.bezierCurveTo(-12, 25, -8, 10, 0, 0); }else{ context.moveTo(-5, 0); context.bezierCurveTo(-10, -10, -10, -30, 0, -40); context.bezierCurveTo(12, -25, 8, -10, 0, 0); } context.closePath(); context.fill(); context.restore(); context.restore(); this.controlStatus(context); } }; $(function(){ RENDERER.init(); });