// 基本语法
var age = 18;
var name = 'Tom';
name2 = 'jary';
var result = true; // false
var number = null;
var number2 = undefined;
// 输出
console.log(age, name, number, number2)
// 变量的真实类型 typeof
console.log(typeof age, typeof name, typeof number, typeof number2)
结果
18 'Tom' null undefined
number string object undefined
** 字符串的拼接**
var newName = name + name2
console.log(newName)
var str1 = 10 + 10 + '10'
var str2 = '10' + 10 + 10
var str3 = (10 + '10') + 10
console.log(str1, str2, str3)
结果
Tomjary
08 - JavaScriptTest.html:34 2010 101010 101010
遵循的规律:
var numbers = [-10, 'san', name , result, number, ['哈哈', '呵呵']]
遍历数组
for(var i = 0; i
数组常用属性
// 删除数组最后一个内容
numbers.pop();
// 最后插入一个
numbers.push('嗷嗷')
找最大值
var numsArr = [10, 212, 2333, 43];
var maxNum = Math.max(20, 21, 2);
var newMaxNum = Math.max.apply(this, numsArr)
function 函数名(参数) {
函数体
}
function sum(a, b) {
return a + b
}
var res = sum(1, 98);
console.log(res);
匿名函数
var result3 = function () {
console.log('匿名函数打印')
}
// 调用
result3()
var dog = {
name: 'wang',
age: 10,
heigh: 1.55,
dogFriends: ['lili', 'huaahua'],
eat: function () {
console.log('吃')
},
run: function () {
console.log('跑')
}
};
console.log(dog.age)
dog.eat()
创建构造函数
var Dog = function () {
this.name = null;
this.age = null;
this.heigh = null;
this.dogFriends = []
this.eat = function (someThing) {
console.log(this.name + '吃' + someThing)
},
this.run = function (someThing) {
console.log(this.name + '跑' + someThing)
}
};
var dog1 = new Dog();
dog1.name = '土狗';
dog1.age = 15;
dog1.dogFriends = ['21', '12'];
var dog2 = new Dog();
dog2.name = '黄狗';
dog2.age = 10;
console.log(dog1, dog2)
Window的作用
var age = 17;
console.log(window.age);
// 动态跳转
window.location.href = 'login.html';
window.location.href = 'XMG2://openCamera?user=';
作用:
document.write('Hello world');
document.write('')
标签修改
车辆
注:Html代码顺序执行,script在head中,document取不到标签
setInterval(() => { }, interval);
window.onload = function() {}
// 鼠标进入图片
img.onmouseover = function () {
console.log('进入图片');
}
// 鼠标在图片上移动
img.onmousemove = function () {
console.log('在图片上移动');
}
// 鼠标离开图片
img.onmouseout = function () {
console.log('离开图片');
}
input.onfocus = function () {}
增
document.write(' Hello world');
var img = document.getElementById('icon')
img.appendChild(img);
删
document.remove();
改
前文提到的取到标签修改
查
getElementById
getElementsBy…