python中scripts_python 学习_第五模块 javascripts

python 学习_第五模块 javascripts

1 js的基本数据类型

//1number 数值类型

var a = 3;var b = 1.23;var c = -123;var d = 0;

alert(typeof a); //number

alert(typeof b); //number

alert(typeof c); //number

alert(typeof d); //number

//2 string 类型

var a = "aaaaa";

alert(typeofa);//Boolean

var c = 3<=4;

alert(c)//true

alert(typeof c) //boolean

//undefined

varx;

alert( x)//undefined

alert(typeof x) //undefined

//空对象

var y = null;

alert(y);//null

alert(typeof y); //object

2 js的加 减 乘 取余 除

运算符

varx= 10;vary= 3;varsum=x+y;

alert(sum)//13

//+ - * % /

//加 减 乘 取余 除

vartest1=x%y;

alert(test1)//1

3 js的递增 递减

递增递减

varx= 4;//x = x + 1;

//x +=1;

x++;

alert (x);//5

varc= 10;

c+= 5;

c-= 2;

c*= 4;

c/= 3;

alert(c);

4 js的字符串

字符串

varstr1= "one";varstr2= "two";varjoined=str1+ ' ' +str2+ 'san'alert(joined)//one twosan

5 js的数值与字符串

数字和字符串

//1 当数字和字符串相加时, 数字也会转换成字符串

vara= "123" + 456alert(a)//123456

alert(typeofa)//string

//2 隐式

varnum= 234;varmyStr=num+ '';

alert(typeofmyStr );//string

//3 toString() 数字转字符串

varmyStr2=num.toString();

alert(typeofmyStr2);//string

//字符串转数值

vara= "123"

varmyNum=Number(a);

alert(typeofmyNum)//number

6 js的数组

数组Array

varshopping=['香蕉','苹果','牛奶','红牛'];//alert(typeof shopping) //object

varrand=['tree','345',[1,2,3]];//访问

varitem1=rand[0];

console.log(item1);//tree

rand[0]= '榴莲';

console.log(rand);vara=rand[2][2];

console.log(a);//3

//访问数组的长度 for

console.log('数组的长度是:' +rand.length);//数组的长度是:3

7 js的条件判断

条件判断

varweather= 'rainy';if(weather==='rainy') {

console.log('天气下雨了,在家里呆着');

}else if(weather==='sunny') {

console.log('天气非常棒,可以出去玩耍')

}else if(weather==='snowing') {

console.log('天气下雪了,可以出去滑雪')

}else{

console.log('输入的天气有错,重新输入');

}

8 js的比较运算符

比较运算符

//=== !== == !=

vara= 5;varastr= '5';varisequal=a===astr;

console.log(isequal)//false

console.log(4 <= 4);

9 js的逻辑运算符

比较运算符

varweaher= 'sunny';vartemp= 22;//&& 逻辑或 并且 || 逻辑或 !true

//&&

if(weaher==='sunny' &&temp>30) {

console.log('在家里吹空调,吃西瓜');

}else if(weaher==='sunny' &&temp<=30){

console.log('天气不错 可以出去玩')

}//||

varmathscore= 77;varenglishscore= 80;if(mathscore>=80 ||englishscore>=85) {

console.log('可以出去玩')

}else{ console.log('在家写作业')}//false

varislogin= false;

alert(!islogin);if(!islogin) {

console.log('用户已登录')

}

10 switch 语句

switch语句

varweather= 'rainy';switch(weather) {case 'summy':

alert(1);break;case 'rainy':

alert(2);//编写 switch语句 小心break case 穿透

break;case 'snowing':

alert(3);break;default:

alert(4);break;

}

11 三元运算符

三元运算符

varresult= 1 < 2 ? '真的':'假的';

alert(result)

12 for循环

for 循环

vari;varsum= 0;for(i=1; i<= 100; i++){

sum+=i;

}

console.log(sum);//5050

varshopping=['香蕉','苹果','牛奶','红牛'];varj;for(j= 0; j' +shopping[j]+ ''console.log(htmlstr);

document.write(htmlstr);

}

13 break和continue

break和continue

//break

varx= 0;for(;;){if(x> 100){break}

x++;

}

alert(x);//continue

varsum= 0;for(vari= 1; i<=10; i++) {if(i===8) {continue;

}

sum=sum+i;

}

alert(sum)

14 while循环

while循环

varsum= 0;//计算的和

varn= 99;//初始的奇数

while(n> 0) {

sum+=n;

n-= 2;

}

alert(sum);//do while 先执行一次, 再判断

varsum= 0;vari= 1;do{

sum=sum+i;

i++;

console.log(sum);

alert(sum);

}while(i<=1);

15 函数

函数

functiontest(isbad ){

alert(isbad);if(isbad){

alert('点个外卖');

}else{

alert('做饭了')

}

}varbad= false;

test(bad);

16 函数返回值和函数表达式

函数返回值和函数表达式

functionadd(a,b){varsum=a+b;returnsum

}functionsub(a,b){returna-b;

}functionmult(a,b){returna*b;

}//function div(a,b){

//return a/b;

//}

//函数表达式

vardiv= function(a,b) {returna/b;

}varr=add(3,2);varr2=sub(3,2);varr3=mult(3,2);varr4=div(3,2);

console.log(r);

console.log(r2);

console.log(r3);

console.log(r4);

17 函数作用域

1

函数作用域

vara= 1;functionadd(){varb= 3;

console.log(a);

}

add();//console.log(b);

2

first.js

(function(){var name = 'ysl';var hello = function(){

alert('hello ' +name);

}

window.first=hello;

})();

second.js

(function(){var name = 'jack';var hello = function(){

alert('hello ' +name);

}

window.second=hello;

})();

函数作用域

console.log(window);

first();

second();

18 对象

object

varperson={

name:'ysl',

age:18,

sex:'男',

fav :function(a) {

alert('爱好姑凉')return '姑凉' +a+ '岁'}

}

console.log(person);

console.log(person.name);

console.log(person.fav(18));

19  js内置函数

js常用的内置对象

varcolors= newArray();//var colors = [];

varcolors2=[];

document.write('1111');//js 提供构造函数

varcolors= newArray();varcolors2=[];if(Array.isArray(colors)) {

colors[0]= 'red';

colors[1]= 'blue';

colors[2]= 'yellow';vara=colors.toString();

console.log(a);

console.log(typeofa);

console.log(colors);

}

fedf5418533841ea05bc7355e6321dab.png

20  数组的方法

1 map filter forEach

map filter forEach

//filter 方法

varnumbers=[1,2,10,7,5,11,44];varfilterResult=numbers.filter(function(item,index,array){

console.log(item);

console.log(index);

console.log(array);returnitem> 10;

});

console.log(filterResult);//map() 方法

varmapresult=numbers.map(function(item,index,array){returnitem*2;

})

console.log(mapresult);for(vari= 0; i

console.log(mapresult[i]);

}//forEach

mapresult.forEach(function(item,index){

console.log(item);

})

2) 数组常用的方法

数组常用的方法

vararr=[1,2,3];vara=arr.toString();//变成string 1,2,3

varb=arr.toLocaleString();//1,2,3

console.log(a);

console.log(b);varperson1={

toLocaleString:function(){return 'mjj';

},

toString:function(){return '么挤挤';

}

}varperson2={

toLocaleString:function(){return '隔壁老王';

},

toString:function(){return '隔壁老李';

}

}varpeople=[person1,person2];

console.log(people);

console.log(people.toString());//么挤挤,隔壁老李

console.log(people.toLocaleString());//mjj,隔壁老王

3)数组常用的方法

数组常用的方法

//分割字符串

varcolors=['red','blue','green'];vara=colors.join('|');

console.log(a);//red|blue|green

//栈 lifo push() pop() 队列方法

//push 相当于 Python的append

varnewlength=colors.push('purple');

console.log(newlength);//4

console.log(colors);//(4) ["red", "blue", "green", "purple"]

//pop() 从数组末尾删除最后一项

varlastItem=colors.pop();

console.log(lastItem);//purple

console.log(colors);//(3) ["red", "blue", "green"]

//队列fifo 先进先出 unshift() shift()

newlength=colors.unshift('yellow');

console.log(newlength);//4

console.log(colors);//(4) ["yellow", "red", "blue", "green"]

varfirstItem=colors.shift();

console.log(newlength);//4

console.log(colors);//(3) ["red", "blue", "green"]

//数组倒序

varvalues=[0,3,2,16,15,10];functioncompare1(a,b){returna-b;

};functioncompare2(a,b){returnb-a ;

};

values.sort(compare1);//升序

console.log(values);//[0, 2, 3, 10, 15, 16]

values.sort(compare2);//降序

console.log(values);//[16, 15, 10, 3, 2, 0]

4 )数组常用的方法

数组常用的方法

//操作方法 concat() slice() splice()

//1 concat 数组合并

varcolors=['red','blue'];//var newColors = colors.concat('green');

newColors=colors.concat({name:'zhangsan'});

console.log(newColors);//2 slice()

varcolors=['red','blue','yellow'];

newcolors=colors.slice(1,2);//["blue"]

console.log(newcolors);//3 splice() 删除 插入 替换

//3.1 删除

varnames=['张三','李四','mjj','alex'];//names.splice(0,2);

//console.log(names); //["mjj", "alex"]

//3.2 插入

//names.splice(1,0,'熊大大','jack');

//console.log(names); //["张三", "熊大大", "jack", "李四", "mjj", "alex"]

//3.3 替换

names.splice(1,1,'xiongdada');

console.log(names);//["张三", "xiongdada", "mjj", "alex"]

//4 位置方法 indexOf() lastIndexOf()

varnames=['张三','mjj','王五','mjj','赵六'];

alert(names.indexOf('mjj'));//1

alert(names.lastIndexOf('mjj'));//3

alert(names.indexOf('mjj',2));//3

alert(names.lastIndexOf('mjj',2));//1

alert(names.lastIndexOf('mjjxxxx',2));//如果查不到结果 返回-1

21 map方法应用

map方法应用

varoldArray=[

{

name:'张三',

age:17},

{

name:'mjj',

age:29},

{

name:'李四',

age:30}

]varnewNames=oldArray.map(function(item,index){returnitem.name

})varnewAges=oldArray.map(function(item,index){returnitem.age

})

console.log(newNames);

console.log(newAges);

22 字符串常用方法

字符串的常用方法

varstr= 'hello world';

console.log(str.length);//11

console.log(str.charAt(1));//e

console.log(str.charCodeAt(1))//101 获取指定的字符对应的编码

console.log(str.concat('mjj','jack'));//拼接字符串 通常情况不适用它来做拼接,使用 +来做多个字符的拼接

console.log(str.slice(2));//llo world

console.log(str.substring(2));//llo world

console.log(str.substr(2));//llo world

console.log(str.slice(2,4));//ll

console.log(str.substring(2,4));//ll

varstr= 'hello world';

console.log(str.indexOf('o'));//4

console.log(str.lastIndexOf('o'));//7

console.log(str.indexOf('o',6));//7

console.log(str.lastIndexOf('o',6));//4

//trim()清除当前 字符串的前后后格

varstr= 'hello world';

console.log(str.trim());//hello world

console.log(str);//hello world

varstr= 'Hello Mjj';

console.log("------",str.toLowerCase());//hello mjj

23 字符串查找

查找当前字符e在字符串中的所有位置

//查找e 在str中的所有的位置

varstr= 'He unfolded the map and set it on the floor.';vararr=[];varpos=str.indexOf('e');//1

console.log(pos);while(pos> -1){//找到当前e字符对应的位置

arr.push(pos);

pos=str.indexOf('e',pos+1);

}

console.log(arr);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值