在这里插入图片描述
js基础![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/95935299e06e4926d2d0c73c26ec987a.jpeg#pic_center)
笔记:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>wwf's web</title>
<style>
</style>
<!-- 内嵌式js
<script>
alert('沙漠骆驼')
</script> -->
<!-- 外部js
<script src="my.js"></script> -->
<!-- <script>
// 输入输出
prompt('请输入您的年龄:')
alert('计算的结果是')
console.log('我是程序员能看到的')
</script> -->
<!-- <script>
//数据类型转换
var num = 10;
var str = num.toString();
console.log(num)
console.log(str)
console.log(typeof str);
//隐式转换
console.log(num + '')
console.log(parseInt('3.14')); //输出3,向下取整
console.log(typeof parseInt(str));
console.log(parseInt('120px')) //120,辉去掉单位px
console.log(parseInt('rem120px')) //NaN
console.log(parseFloat('3.14'));
console.log(Number('12'));
console.log('21' - 32); //- * /
console.log('123' - '120');
console.log('123' * 1);
var a = '12';
var b = '12';
console.log(a / 1 + b / 1);
console.log(Boolean(''));
console.log(Boolean(0)); //NaN、null、undefined
</script> -->
<!-- <script>
var e = 10;
console.log(e++ + ++e); //22、e++=10 -> e=11 -> e=12 -> ++e=12
</script> -->
<!-- <script>
//逻辑与&&的短路运算(逻辑中断)
console.log(123 && 456); //456 如果表达式1为真,则返回表达式2
console.log(0 && 456 && 2 * 3232); //0 如果表达式1为假,则返回表达式1
//逻辑或短路运算
console.log(123 || 456); //123 如果表达式1为真,则返回表达式1
console.log(0 || 456 || 789); //456 如果表达式1为假,则返回表达式2
//小例子
var num = 0;
console.log(123 || ++num);//++num未执行
console.log(num);
</script> -->
<!-- <script>
//== he ===的区别
console.log(2 === '2'); //false
console.log(2 == '2'); //true
</script> -->
<!-- <script>
//while循环
var msg = prompt("你爱我吗?");
while (msg != '我爱你') {
msg = prompt("你爱我吗?");
}
alert('好的,你可以访问此页面了!');
</script> -->
<!-- <script>
//数组:
//var arr = new Array();
var arr = [];
var arr = [1, 2, 'wwf', true];
console.log(arr[4]); //undefined
arr.length = 5; //修改数组长度
var arr1 = [1, 32, 45, 2, 5, 64, 3];
var arr2 = [];
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] > 10) {
arr2[arr2.length] = arr1[i];
}
}
console.log(arr2);
</script> -->
<!-- <script>
//arguments,函数内置对象,以伪数组形式存储,
//1.具有length属性,2.按索引方式存储,3.没有真正数组的一些方法,如pop、push等
function fn() {
console.log(arguments); //存储了所有传递过来的实参
console.log(arguments.length);
}
fn(1, 2, 3);
function getMax() {
var max = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
console.log(getMax(1, 2, 32, 3, 4, 5, 3, 534, 53));
//冒泡排序: ?????函数表达式(匿名函数)
var bS = bubbleSort(arr) {
for (var i = 0; i < arr.length - 1; i++) {
for (var j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
console.log(bS([1, 23, 32, 3, 9, 45, 4, 6]));
</script> -->
<!-- <script>
//全局作用域
//局部作用域
//es6新增块级作用域{}、for{}、if{}
if (3 < 5) {
var num = 10;
}
console.log(num);
//作用域链
var num = 1;
function fn1() {
var num = 2;
fn2();
function fn2() {
var num = 3;
console.log(num);
}
}
fn1();
</script> -->
<!-- <script>
//预解析:js引擎会把所有的var和function放到当前作用域的最前面
//1.变量提升,不提升赋值操作
//会输出undefined
console.log(num);
var num = 10;
//2.函数提升,不调用函数;调用必须写在函数表达式的下面
//结果为99999报错
f1();
console.log(c);
console.log(b);
console.log(a);
function f1() {
var a = b = c = 9;
console.log(a);
console.log(b);
console.log(c);
}
// //相当于:
// function f1() {
// var a;
// a = 9;
// b = 9;
// c = 9;
// console.log(a);
// console.log(b);
// console.log(c);
// }
// f1();
// console.log(c);
// console.log(b);
// console.log(a);
</script> -->
<!-- <script>
//字面量创建对象
var obj = {
uname: '',
age: 20,
sayHi: function() {
console.log('hi');
}
}
obj.sayHi();
console.log(obj['age']);
//利用new Object创建对象
var obj2 = new Object();
obj2.uname = 'wwf';
//利用构造函数创建对象
//构造函数名首字母要大写;构造函数不用return就可以返回结果;调用构造函数必须使用new
function Star(uname, age, sex) {
this.uname = uname;
this.age = age;
this.sex = sex;
this.sing = function(song) {
console.log(song);
}
}
var ldh = new Star('andyL', 18, 'man');
ldh.sing('iceRain');
for (var k in ldh) {
console.log(ldh[k]);
}
</script>-->
<!-- <script>
//数学对象Math
console.log(Math.floor(1.6)); //向下取整
console.log(Math.ceil(1.4)); //向上取整
console.log(Math.round(1.5)); //四舍五入
console.log(Math.round(-1.5)); //-1// .5 特殊,往大取,-1大
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandom(20, 30));
</script> -->
<!-- <script>
//Date方法
var date = new Date();
console.log(date); //现在的时间
console.log(Date.now()); //时间戳
var nowTime = +new Date(); //当前时间戳
console.log(nowTime);
</script> -->
<!-- <script>
//数组补充
var arr = [1, 2, 3];
var obj = {};
console.log(arr instanceof Array); //instanceof 运算符
console.log(Array.isArray(obj));
console.log(arr.push(4, 'wwf')); //返回新数组的长度
console.log(arr);
console.log(arr.unshift(0)); //在数组前面添加元素
console.log(arr);
console.log(arr.pop()); //删除数组的最后一个元素
console.log(arr);
console.log(arr.shift()); //删除数组的第一个元素
console.log(arr);
//冒泡排序
var arr2 = [1, 90, 34, 47, 7];
arr2.sort(function(a, b) {
return a - b; //升序排列
//return b-a;//降序排列
})
console.log(arr2);
var arr3 = ['blue', 'red', 'blue'];
console.log(arr3.indexOf('blue')); //返回满足第一个条件的索引,没有则返回-1
console.log(arr3.indexOf('blue', 1)); //查找第二个blue
console.log(arr3.lastIndexOf('blue')); //返回最后一个满足条件的索引
//数组转化为字符串
console.log(arr3.toString());
console.log(arr3.join(' and '));
//连接数组
var arr4 = arr2.concat(arr3);
console.log(arr4);
//截取数组
var arr5 = arr4.slice(4, 6);
console.log(arr5); //(start,end)
console.log(arr4); //不影响原数组
var arr6 = arr4.splice(4, 2); //(开始索引数,删几个)//字符串用substr
console.log(arr6);
console.log(arr4); //修改了原数组
</script> -->
<script>
//字符串
var str = 'andy';
console.log(str.charAt(3)); //返回字符
console.log(str.charCodeAt(3)); //返回阿斯克码//判断用户按了哪个键
console.log(str[3]);
var str2 = 'red,blue,yellow'
console.log(str2.split(',')); //字符串转数组
</script>
<!-- <script>
//null返回的是一个空对象,如果有个变量以后打算存储为对象,暂时没想好放什么,就给null
</script> -->
</head>
<body>
<h2>javascript的学习</h2>
<p>
<!-- 行内式的js:-->
<!-- <input type="button" value="唐伯虎啊·" οnclick="alert('点秋香')">-->
</p>
</body>
</html>