2021.06.16(practice)

+true;
!"Lydia";

result:+号会将true变为1,+号倾向于返回一个值,但是!倾向于返回一个布尔值,因为"Lydia"是真实存在的,所以取反之后是false。

 

const bird = {
  size: 'small'
}
const mouse = {
  name: 'Mickey',
  small: true
}

result:因为mouse通过点的形式调用bird的前提是mouse有这个属性,但是它没有,所以是无效的,但是bird.size是真实存在的,通过[]调用里面存放的是small这个字符串

 

 

let c = { greeting: 'Hey!' }
let d

d = c
c.greeting = 'Hello'
console.log(d.greeting)

 

result:输出是 'Hello',因为在JS中对象是一种引用类型,d和c指向的是同一片内存区域,所以对c进行修改,d指向的内容也发生了改变。

 

let a = 3
let b = new Number(3)
let c = 3

console.log(a == b)
console.log(a === b)
console.log(b === c)

reuslt:输出是 true、false、false。之所以是这样,当我们是用==符号时,系统会检测两边是否有相同的值,有时候还会对值进行转换的比较,但是如果我们使用===符号时,不仅要求两边要有相同的值,还要求有相同的类型,new Number出来的是一种对象,其身上不仅仅有值,还有一堆额外的功能。

 

class Chameleon {
  static colorChange(newColor) {
    this.newColor = newColor
    return this.newColor
  }

  constructor({ newColor = 'green' } = {}) {
    this.newColor = newColor
  }
}

const freddie = new Chameleon({ newColor: 'purple' })
freddie.colorChange('orange')

result:输出是TypeError,之所以是错误,原因在于Chameleon是一个类,colorChange是类中的静态方法,类中的静态方法只能被构造器使用,不能被实例进行调用。

 

let greeting
greetign = {} // Typo!
console.log(greetign)

result:输出的是{},表面上看greetign存在拼写错误,应该报错才对,但是这是在全局作用域下的,实际上是进行了赋值的,所以打印出的是{}.

 

function bark() {
  console.log('Woof!')
}

bark.animal = 'dog'

result:正常运行!之所以会这样,是因为JS中的函数也是一类特殊的对象,所以也可以通过点的形式添加属性。

 

function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const member = new Person("Lydia", "Hallie");
Person.getFullName = function () {
  return `${this.firstName} ${this.lastName}`;
}

console.log(member.getFullName());

result:TyprError  Person是一个构造函数,不能像普通的对象给构造函数添加属性,要想给构造函数的实例添加属性,可以通过原型的方式来实现。

 

DOM事件流的三个阶段分别是事件捕获阶段、处于目标阶段、事件冒泡阶段。事件捕获的三个阶段图示:

 

 

2021-06-17   09:32:58

 

posted @ 2021-06-17 09:33  铁打的代码流水的bug  阅读(62)  评论(0)    收藏  举报