会陆续更新
1.变量声明
var a;
变量
var b = 100
变量名必须是英文字母、_、$开头
必须是英文字母、_、数字结尾
不可用系统的关键字保留字作为变量名
2.数据类型
运算的优先级大于赋值
js天生浮点型
原始值 stack 栈 不可改变, 重新赋值就是在新的房间重新定义了新值,之前的那个回复房间号,和磁盘一样,不会被永久删除,只是会覆盖之前的数据
Number
String
Boolean
undefined
null
Symbol
var a = 10
var b = a
a = 20
console.log(a,b) a = 20 , b = 10
值存放在底部,先进后出
引用值 heap 堆
Object
Array
function
var arr = [1.2]
var arr1 = arr
arr.push(3)
结果都会变
var arr = [1,2]
var arr1 = arr
arr = [1,3] 在堆里面开辟了一个新的值
打印结果 arr [1,3] arr1 [1,2] 结果不一样
字符串和任何东西相加都是字符串类型的
Infinity + 1 == Infinity true
& 是表示位的与运算,把左右两边的数字转换成为二进制,然后每一位分别进行比较,如果相等就返回1,不相等就为0,同时具有强制转换的功能,把false,true转换成0和1进行比较
计算2的n次幂
let n = parseInt(window.prompt('2的n次幂'))
let m = 1
for(i = 0; i < n; i++){
m *=2
}
console.log(m)
5的阶乘
let a = 1
for(i = 1; i <= 5; i++){
a *=i
}
打印 a
比较三个数中最大的一个
var a, b, c;
// a = 1;
// b = 1;
// c = 1;
// var max = 0;
// if (a >= max) {
// max = a;
// }
// if (b >= max) {
// max = b;
// }
// if (c >= max) {
// max = c;
// }
// if (a > b) {
// if (a > c) {
// console.log(a);
// } else {
// console.log(c);
// }
// } else {
// if (b > c) {
// console.log(b);
// } else {
// console.log(c);
// }
// }
// console.log(max);
// 费波那契数列
// let a = 1,
// b = 1,
// c;
// for (let i = 0; i < 5; i++) {
// c = a + b;
// a = b;
// b = c;
// }
// console.log(c);
// function fb(n) {
// if (n == 1 || n == 2) {
// return 1;
// }
// return fb(n - 1) + fb(n - 2);
// }
// console.log(fb(5));
// 1-100以内的质数
// for (let i = 2; i <= 100; i++) {
// var falg = true;
// for (let j = 2; j < i; j++) {
// if (i % j == 0) {
// falg = false;
// }
// }
// if (falg) {
// console.log(i);
// }
// }
构造函数不能返回原始值
// var str = "abc";
// str += 1;
// var test = typeof str;
// console.log(test.length == 6);
// if (test.length == 6) {
// test.sign = "9999999";
// // 原始值赋值没啥卵用
// }
// console.log(test.sign); // 重新包装了一下 test.sign undefined
原型,在出生的时候就被定义好了
绝大多数对象的最终的会继承Object.prototype
var b = Object.create(null) 没有原型
Math.ceil(0.1) 1 向上取整
Math.floor(9.9) 9 向下取整
function Person(name,age){
this.name = name
this.age = age
}
var person = new Person()
var obj = {
name:123
}
Person.call(obj,'张三',18)
obj继承了Person的属性,自身的会被覆盖了
function Wheel(style,wheelSize){
this.style = style
this.wheelSize = wheelSize
}
function Sit(c,sitColor){
this.c= c
this.sitColor = sitColor
}
function Model(height,width,len){
this.height= height
this.width = width
this.len = len
}
function Car(whellArr,c,sitColor,height,width,len){
Wheel.apply(this,whellArr)
Sit.call(this,c,sit,Color)
Model.call(this,height,width,len)
}
var whellArr = [1,2]
var car = new Car(whellArr,3,4,5,6,7)
call apply 改变this指向 传参列表不同
call 一个一个参数传
apply 那参数放在数组里,传递的是一个数组
继承 圣杯模式
Father.prototype = {
name: '张三'
}
function Father(){}
function Son(){}
function inherit(Target,Orgin){
function F(){}
F.prototype = Orgin.prototype
Targit.prototype = new F()
Targit.prototype.constuctor = Target // 让他原型等于他自己
Targit.prototype.uber = Orgin.prototype // 最终继承的是谁
}
inherit(Son,Father)
Son.prototype = {
sex:'男'
}
var son = new Son()
var father = new Father()
单独修改son的原型
var inherit = (function(){
var F = function(){}
return function (Target,Orgin){
F.prototypr = Orgin.prototype
Target.prototype = new F()
Targin.prototype.constutor = Target
Target.prototype.uber = Orgin.prototype
}
})()
inherit(Son,Father)
// 命名空间
// var deng = {
// smok: function () {
// console.log('抽烟')
// return this
// },
// drink: function () {
// console.log('喝酒')
// }
// }
// console.log(deng.smok().drink()) 连续调用方法
// var obj = {
// a: 1,
// b: 2,
// __proto__: {
// lastName: '张'
// }
// }
// for (let prop in obj) {
// if (obj.hasOwnProperty(prop)) { // 不加判断会把原型的属性也遍历出来
// console.log(obj[prop])
// }
// }
// console.log('a' in obj) // 判断 'a' 属性是否属于obj 原型上的也是
// A instanceof B A对象是否是B构造函数构造出来的 *看A的原型链上有没有B的原型
// function Person() { }
// var person = new Person()
// console.log(Array instanceof Object)
// console.log(Array.isArray([]))
// function isArrObj(value) {
// if (Object.prototype.toString.call(value) == "[object Object]") {
// console.log('是对象')
// } else if (Object.prototype.toString.call(value) == "[object Array]") {
// console.log('是数组')
// } else if (Object.prototype.toString.call(value) == "[object Null]") {
// console.log('是null')
// }
// }
// console.log(isArrObj(null))
// console.log([] instanceof Object) // true
// console.log({} instanceof Array) // false
// constructor a.constructor 知道 a 是谁构造的
// instanceof A对象是否是B构造函数构造出来的 *看A的原型链上有没有B的原型
// toString() 查看变量的数据类型 是真的准
// var arr = []
// Array.isArray(arr) 判断是否是数组
字符串
// 字符串
// length 返回字符串的长度
// var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// var sln = txt.length
// indexOf() 方法返回字符串中指定文本首次出现的索引位置
// var pos = txt.indexOf('B')
// lastIndexOf() 文本在字符串中最后一次出现的索引
// var pos = str.lastIndexOf('B')
// 如果没有找到文本 indexOf() 和 lastIndexOf() 均返回 -1
// 都支持第二个参数,检索的索引开始位置
// search() 方法搜索特定的字符串,并返回匹配的位置
// 两种方法,indexOf() 与 search() ,是相等的。
// 这两种方法是不相等的。区别在于:
// search() 方法无法设置第二个开始位置参数。
// indexOf() 方法无法设置更强大的搜索值(正则表达式)。
// 提取部分字符串
// 所有字符串方法都会返回新字符串,他们不会修改原始字符串
// 字符串是不可改变的,字符串不能更改,只能替换
// 有三种办法
// slice(start, end)
// substring(start,end)
// substr(start, length)
// slice() 提取字符串的某部分并在新字符串中返回被提取的部分
// var str = '923456789'
// var res = str.slice(3, 6)
// console.log(res) 456 包后不包前
// var res = str.slice(-5, -1); '5678'
// 只有一个参数 -1 截取最后一位
// console.log(str.replace('9', 0))
// 匹配首个出现的元素
// toUpperCase() 把字符串转换为大写
// toLowerCase() 把字符串转换为小写
// concat() 连接两个或多个字符串 方法可用于替代加运算符
// trim() 方法删除字符串两端的空白字符串
// charAt() 方法返回字符串中指定下标(位置)的字符串
// var str = "HELLO WORLD";
// str.charAt(0); // 返回 H
// charCodeAt() 返回字符串中指定索引的字符串unicode编码
// var str = "HELLO WORLD";
// str.charCodeAt(0); // 返回 72
// var str = 'HELLO WORLD'
// str[0] H
// split() 把字符串转换成数组