javaScripts 知识点一

基础变量类型与变量

1,JavaScript都有哪些变量类型?

  1. number
  2. string
  3. undefined
  4. null
  5. object
  6. boolean

2,类型判断?

const { log } = require('console');

/**
 * typeof 可以用来判断大部分类型
 */
let a;
log('typeof 123: ', typeof 123); //number
log('typeof "hello world:" ', typeof 'hello world'); //string
log('typeof false:', typeof false); //boolean
log('typeof a: ', typeof a); // undefined
log('typeof null: ', typeof null); //object

/**
 * 对应引用类型 我们一般使用instanceof
 */

const test1 = function () {};
const test0 = new test1();
const array1 = [];
log({} instanceof Object); //true
log(array1 instanceof Array); //true
log(Array.isArray(array1)); //true
log(test0 instanceof test1); //true

/**
 * 更加准确的判断null  因为 null 如果使用typeof null  会是 object
 * 我们可以使用恒等式 === 判断
 */
const null1 = null;
log(null1 === null); //true

/**
 * 更加准确的看出引用类型 我们可以使用 Object.proptotype.tostring.call
 */
log(Object.prototype.toString.call(null)); //[object Null]
log(Object.prototype.toString.call(array1)); //[object Array]
log(Object.prototype.toString.call(test1));//[object Function]

什么是变量提升?

变量提升指的是会把变量或函数的声明移到它们执行之前。

  1. let、const 不存在提升,因为let、const声明的变量会存在一个暂时性的死区。
  2. 函数表达式不能被提升。
  3. var可以提升。
  4. 函数声明可以提升。
const { log } = require('console');

//var 变量提升
log(a);
var a = 100;
log(a);

/**
 * let const 声明的变量是没法提升的因为 let声明的变量处于暂时性的死区
 */
// log(b);
// let b = 200;
// log(b);

//函数提升
test();
function test() {
  log('test function');
}
/**
 * 函数表达式不能提升
 * test1 is not undefined
 */
// test1();
// test1 = function () {
//   log('test1 function');
// };

typeof NaN 会返回什么? 什么时候回出现NaN?

首先我们给出答案  typeof NaN 的话 返回的是number。因为NaN是not-a-number的一个缩写,是number中的一个特殊数字结果,可以理解为是一个无效的数字。

当我们把一个字符串转成数字的时候,就会出现NaN。Number('123')。

我们把一个负数进行平方根 也会出现 NaN。Math.sqrt(-1)。

typeof  null 为什么返回的是 Object?

因为这是一个历史遗留问题。我们可以通过恒等符号 === 来判断,或者通过 Object.prototype.tostring.call 来判断。

null 和undefined 的区别是什么?

1,null 和undefined 都是表示没有值。

2,null 是可以赋值给一个变量,属于Object 类型。undefined 是当一个变量没有赋值时,则该变量会是一个undefined。

3,null == undefined  等于true。使用 null===undefined 是 等于false。

console.log([] == false)输出什么?

结果是 true

因为宽松等于 会把 [] 转换成0,false会转换成 0  所以输出结果是true。

扩展:

//类型的强制转换

const { log } = require('console');

log([] == false); //true   []会强制转成 0  false强制转成 0 所以输出会是一个 true
log([] == ''); // true  原理同上

log([1, 2, 3] == '1,2,3'); //true  [1, 2, 3]强制转成 '1,2,3'
log([1, 2] == '1,2'); //true  [1, 2]强制转成 '1,2'

log([1] == 1); // true  [1] 会转换成1
log([0] == 0); // true  [1] 会转换成1
log([123] == 123); //true [123] 会转换成 123

log(5 == '5'); //true  '5'会转换成 5

log(true == 1); // true true会被转换成 1
log(false == 0); // true  false 会被转换成 0

== 和 === 的区别?

== 是宽松等于,会进行类型转换。

=== 是严格等于,会同时进行值和类型的对比。

const、let、var 区别?

  1. const 、let 属于块级作用域,变量不会被提升。
  2. let 声明的变量可以重新赋值,const变量不能被重新赋值,但是可以修改对象的属性。
  3. var属于函数作用域或全局作用域,不推荐使用。
// const  let var 的区别?

const { log } = require('console');

/**
 * const let 属于块级作用域,变量不能被提升,属于暂时性的死区。
 * const 声明的变量不可以修改,但是可以修改对象的属性
 * let 可以重新赋值
 * var 属于函数作用域或者全局作用域,变量可以提升。
 */

//块级作用域示例
if (true) {
  var a = 990;
}
log(a); //输出 990

// if (true) {
//   let b = 100;
//   const c = 200;
// }
// log(b, c); //b、c  is  not defined

//变量提升示例
log(d);
var d = 300;
log(d);

log(dd);
let dd = 400;

const 

const 声明的变量一定不能被修改吗?

声明的变量如果是值类型是不可以被修改的,如果是对象类型,引用对象不能被修改,对象的属性可以被修改。

const声明的数组,可以push吗? 为什么?

const声明的变量的引用不能被修改,意味着不能把新的数组赋值改变量,但是数组的内部数据可以被修改。允许数组内部数据可以添加、删除、更改。

const 声明的对象,如何使其不被修改?

可以使用Object.freeze 冻结对象。但是只能冻结第一层

// const 声明了数组,还可以push吗? 为什么?
/**
 * const 声明的数组可以push,但是引用的数组不能被修改,数组的内部可以修改
 */

const { log } = require('console');

//const 声明了一个对象,如何保证对象不被修改
const test = [];
Object.freeze(test);
// test.push(123);
log(test);

const test1 = {
  name: 'zhangsan',
  detail: {
    age: 20,
    coin: 200,
  },
};
//对象属性修改测试
test1.name = 'lisi';
log(test1);
//冻结测试
Object.freeze(test1); //只能冻结对象的第一层
test1.name = 'wangwu';
test1.detail.age = 30;
log(test1);
//深度冻结测试
function deepFreeze(obj) {
  const pros = Object.getOwnPropertyNames(obj);
  pros.forEach((name) => {
    const tempObj = pros[name];
    if (typeof tempObj === 'object' && tempObj !== null) {
      deepFreeze(tempObj);
    }
  });
  return Object.freeze(obj);
}
deepFreeze(test1);
test1.name = 'testObj';
test1.detail.age = 30;
log(test1);

这两种方式的区别 ?typeof 判断(字节)

const str1 = "abc";
const str2 = new String("abc");

第一种是字面量创建字符串,第二种是使用构造函数创建。

typeof str1 =》 string     属于原始字符串  推荐使用。

typeof str2 =》 Object   属于对象类型  不是原始字符串。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值