js变量类型和原型

变量类型和计算

  • js中使用typeof能得到的哪些类型
    • typeof undefined //undefined
    • typeof “abc” //string
    • typeof 123 //number
    • typeof true //boolean
    • typeof {} //object
    • typeof null //object
    • typeof [] //object
    • typeof console.log //function
  • 何时使用=== 何时使用==
    • if (obj.a == null){

      ​ //相当于obj.a === null || obj.a === undefined , 简写形式。 这是jquery源码中推荐的写法

      ​ //其他情况都用 ===

      }

  • js中有哪些内置函数
    • Object
    • Array
    • Boolean
    • Number
    • String
    • Function
    • Date
    • RegExp
    • Error
  • js变量按照存储方式区分为哪些类型,并描述其特点
    • 基本数据类型
      • 值是存在栈内存中
    • 应用数据类型
      • 值是存在堆内存中,如果存在着负值,那得到的只是一个指针
  • 如何理解JSON
    • JSON只不过的一个js对象而已

      • 常用方法

        JSON.stringify({a:10, b:20})

        JSON.parse(’{“a”:10, “b”:20}’)

  • 如何准确判断一个变量是数组类型
    • instanceof Array
  • 写一个原型链继承的例子
    • 所有的引用类型(数组,对象,函数),都具有对象特性,即可自由扩展属性(除了“NULL”意外)

    • 所有的引用类型(数组,对象,函数),都有一个__proto__属性,属性值是一个普通对象

    • 所有的函数,都有一个prototype属性,属性值也是一个普通对象

    • 所有的引用类型(数组,对象,函数),__proto__属性值指向它的构造函数的“prototype”属性值

    • 当试图得到一个对象的某个属性时,如果这个对象本身没有这个属性,那么会去它的__proto__(即它的构造函数的prototype)中寻找。

      // 动物
      function Animal(){
              
              
          this.eat = function() {
              
              
              console.log('animal eat');
          }
      }
      
      // 狗
      function Dog(){
              
              
          this.bark = function() {
              
              
              console.log('dog bark')
          }
      }
      Dog.prototype = new Animal()
      // 哈士奇
      var hashiqi = new Dog()
      

      hasOwnProperty() 方法会返回一个布尔值,指示对象自身属性中是否具有指定的属性(也就是,是否有指定的键)。

  • 描述NEW一个对象的过程
    • 创建一个对象
    • this指向这个新对象
    • 执行代码,即对this赋值
    • 返回this
  • zepto(或其他框架)源码中如何使用原型链
    // 构造函数
    function DomElement(selector) {
          
          
        var result = document.querySelectorAll(selector)
        var length = result.length
        var i
        for (i = 0; i < length; i++) {
          
          
            this[i] = selectorResult[i]
        }
        this.length = length
    }
    // 修改原型
    DomElement.prototype = {
          
          
        constructor: DomElement,
        get: function (index) {
          
          
            return this[index]
        },
        forEach: function (fn) {
          
          
            var i
            for (i = 0; i < this.length; i++) {
          
          
                const elem = this[i]
                const result = fn.call(elem, elem, i)
                if (result === false) {
          
          
                    break
                }
            }
            return this
        },
        on: function (type, fn) {
          
          
            return this.forEach(elem => {
          
          
                elem.addEventListener(type, fn, false)
            })
        }
    }
    
    // 使用
    var $div = new DomElement('div')
    $div.on('click', function() {
          
          
        console.log('click')
    })
    

猜你喜欢

转载自blog.csdn.net/qq_39208971/article/details/108219846