typeof只能判断简单的数据类型,typeof返回的是字符串,所以typeof typeof string返回的是string
判断arr和object
array.isArray(arr),数组有长度,对象无
map有返回值,foreach没有。
浅拷贝复用地址,深拷贝开辟一个新地址
在两个操作数中只要 有一个是字符串类型,那么+就是字符串运算符,而不是算数运算符(注意只要两个,不会影响三个以上例如2+3+"5"输出结果为55,而不是235)
ES6版本 : 类
一、类里属性的定义 es6类里的方法不支持(方法:function)的写法,只支持简写
class 类名{
constructor(属性){
this.属性 = 属性;
}
}
class 类名{
this.属性 = 属性;
constructor(){
}
}
class 类名 {
get 属性 () {
return 返回值;
}
set 属性 (newvalue) {
console.log(newvalue);
}
}
应用
let 应用名 = new 类名(输入值);
console.log(应用名);
二、类的方法定义
class 类名{
constructor(属性){
this.属性 = 属性;
}
函数名 (){
//
}
}
class 类名{
constructor(属性){
this.属性 = 属性;
}
函数名 = ()=>{
//
}
}
应用
let 应用名 = new 类名(输入值);
应用名.函数名();
一、私有成员 : 1.私有属性 2.私有方法;
public : 所有位置都可以调用
protected : 只能自身和子类调用 外部不能调用
private : 自能自身类调用
原生js的类 : 1.公有 2.私有 ;
1.公有属性和方法
class 类名{
constructor(属性){
this.属性 = 属性; // 公有属性 ,所有位置都可以调用
}
函数名 (){
console.log("函数名",this.属性); // 内部调用
}
}
let 应用名 = new 类名();
应用名.函数名(); // 外部调用
2.私有成员 。 通过 # 号修饰符修饰;
私有成员:只能类的内部调用 ;
class 类名{
#属性 = "值"; // 私有属性
constructor(){
this.属性 = 属性;
}
#函数名1(){ // 私有方法;
//
}
函数名2(){
console.log(this.#属性); // 内部调用属性
this.#函数名(); // 内部调用方法;
}
}
let 应用名 = new 类名();
console.log(应用名.#函数名1); // 类的外部不能调用私有成员;
zhangsan.函数名2(); // 通过公有方法调用私有成员;
二、静态成员
静态成员: 成员属于 类的成员 ,不属于 对象的成员;
特点 : 不需要实例化的;
class 类名{
static 属性 = 值; // 静态属性 ;
constructor(属性){
this.属性 = 属性;
}
static 函数名(){ // 静态方法;
//
}
}
console.log(类名.静态属性);
类名.静态方法();
Array.isArray();
单例模式 ,单体模式 : 一个类 只能实例化一个对象出来 ;
class 类名{
// 通过静态成员记录实例化的状态 ,第一次实例化的时候把 第一次的实例 保存起来,第二次 直接把第一次的实例抛出;
static instance;
constructor(属性){
this.属性 = 属性;
if(!类名.instance){
类名.instance = this;
}else{
return 类名.instance;
}
}
}
ES6的继承
#是private的关键字,不加默认为public
父类
class 类名{
constructor(属性){
this.属性 = 属性;
}
父类函数名(){
console.log("我是父类的方法");
}
}
子类
class 子类类名 extends 父类类名{
}
如果子类需要扩充属性和方法 ,需要用到 constructor 那么一定需要调用super函数
super使用的时候注意点
1.如果省略 constructor 那么super 也可以省略
2.super 不能单独存在 ,必须在子类里
3.super 的调用一定要在使用this之前;
class 父类类名{
constructor(父类属性){
this.父类属性 = 父类属性;
}
父类方法(){
console.log("父类方法");
}
}
class 子类类名 extends 父类类名{
constructor(父类){
super(父类属性);
this.增加的属性 = 增加的属性值;
}
子类方法(){
console.log("子类fn函数");
super.父类方法();
}
}
调用
let 调用名 = new 子类类名(输入值);
console.log(调用名);
调用名.子类方法();
js里的随机数
Math.random(); // 随机 0 --- 1 的数
console.log(Math.random());
console.log(parseInt( Math.random()*10)); // 0---10
console.log(parseInt( Math.random()*10) + 10); // 10---20
console.log(parseInt( Math.random()*10) + 15); // 15---25
console.log(parseInt( Math.random()*(max-min)) + min);
// min --- max
function randomNum(min,max){
return parseInt( Math.random()*(max-min))+min;
}
console.log(randomNum(0,3));