Javasricpt总结(三)

判断类型的基本方法:
1.typeof 运算符返回一个字符串,表示操作数的类型

console.log(typeof 42);
// Expected output: "number"

内存泄露是指程序运行时,分配的内存没有被正确释放,导致内存空间的不断积累,最终导致程序性能下降或者程序崩溃。

2.instanceof 运算符用于检测构造函数的prototype属性是否出现在某个实力对象的原型链上。

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);

console.log(auto instanceof Car);
// Expected output: true

console.log(auto instanceof Object);
// Expected output: true

3.constructor属性返回Object的构造函数(用于创建实例对象)

const a = new Array
a.constructor === Array // true
const o = {}
o.constructor === Object // true
let b = [];
b.constructor = String
b.constructor === String // true
b instanceof String // false
b instanceof Array // true

4.Object.prototype.toSrting toString() 方法返回一个表示该对象的字符串

const arr = [1, 2, 3];
arr.toString(); // "1,2,3"
Object.prototype.toString.call(arr); // "[object Array]"

const theDog = new Dog("Gabby", "Lab", "chocolate", "female");
theDog.toString(); // "[object Object]"
`${theDog}`; // "[object Object]"

字符串全局替换方法:添加'/g'为全局替换

let str = 'afjkaafdjkjajjsaa';
let result = str.replace(/a/g,'A')
//AfjkAAfdjkjAjjsAA

冒泡排序法的几种常用方法:
let arr = [1,3,2,1,6,8,5,9,4,0,2,5];
1.sort方法:
(1)a>b,返回正数;
(2)a=b,返回0;
(3)a<b,返回负数,在重新排序后的数组中a位于b之前;
(4)a-b,返回数组从小到大
(5)b-a,返回数组从大到小

arr.sort((a,b)=> a-b);
//[0, 1, 1, 2, 2, 3, 4, 5, 5, 6, 8, 9]

2.for循环

for(let i = 0; i < arr.length; i++){
  for(let j = i+1; j < arr.length; j++){
    if(arr[i] > arr[j]){
        let val = arr[i];
        arr[i] = arr[j];
        arr[j] = val;
    }
  }
}
//[0, 1, 1, 2, 2, 3, 4, 5, 5, 6, 8, 9]
posted @ 2023-02-27 17:39  夏季的雨  阅读(49)  评论(0)    收藏  举报