JS基础知识

Js 变量类型和计算

  1. 变量类型
    值类型和引用类型

    var a = 100
    var b = a
    a =200
    console.log(b) //100
    
    var a = {age:20}
    var b = a
    b.age = 21
    console.log(a.age)//21

    引用类型:对象 数组 函数
    内置函数有:object array boolean number string function date regexp error

    typeof

    typeof undefined  //nudefined
    typeof 'abc' //string
    typeof 123  //number
    typeof true //boolear
    typeof {} //object
    typeof [] //object 
    typeof funtion // function
  2. 变量计算
    强制类型转换

    • 字符串拼接

      var a = 100 + 10   //110
      var b = 100+ '10'  //'10010'
    • == 运算符

      100 == '100' //true
      0 == '' //true
      null == undefined //true

      何时使用 === 何时使用 ==
      if(abj.a == null) 相当于 obj.a ===null obj.a ===undefined jquery推荐 其他时候用 ===

    • if语句

          var a = true
          if (a){}
          var a = true
          if (a){}
          var a = true
          if (a){}
    • 逻辑运算

          console.log(10&&0) //0
          console.log(''||'abc')  //'abc'
          console.log(!window.abc)  //true
          // 判断一个变量会当做true 还是 false
          var a =100
          console.log(!!a)

Json

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


构造函数

原型和原型链

  1. 构造函数

        fuction Foo(name,age){ //构造函数首字母大写
            this.name =name
            this.age =age
            this.class = 'clas-1'
            // return this //默认有这样
        }
        var f = new Foo('zhangsan',20) //创建多个对象
  2. 构造函数-拓展
    var a = {} 是var a = new Object() 的语法糖
    var a = {} 是var a = new Array() 的语法糖
    function Foo(){} 是 var Foo = new Function(){}
    使用instanceof 判断一个函数是否是一个变量的构造函数

  3. 原型规则
    - 所有的引用类型(数组,对象,函数),都具有对象特征,即可自由拓展属性(除了null以外。)
    - 所有的引用类型(数组,对象,函数),都有一个proto,属性值是一个普通的对象。引用类型
    - 所有的函数,都有一个prototype属性,属性值也是一个prototype属性,属性值也是一个普通对象。显示原型
    - 所有的引用类型(数组,对象,函数),proto属性值指向它的构造函数“prototype”属性值。
    - 当试图得到一个对象的某个属性时,如果这个对象本身没有这个属性,那么回去他的proto(即他的构造函数的prototype)中寻找。

        var obj = {}; obj.a = 100;
        var arr = [];  arr.a = 100;
        function fn(){}
        fn.a = 100;
        console.log(obj.__proto__);
        console.log(arr.__proto__);
        console.log(fn.__proto__);
        console.log(fn.prototype);
        console.log(obj.__proto__=== Object.prototype)
        //示例
        function Foo(name,age){
            this.name =name
        }
        Foo.prototype.alertName = function(){
            alert(this.name)
        }
        //创建示例
        var f = new Foo('zs')
        f.printName = function(){
            console.log(this.name)
        }
        f.alertName()
        f.printName() 

    this 永远指向对象本身。
    循环对象自身的属性
    for(item in object){}
    只想拿到本身的属性 if(f.hasOwnProperty(item){}

  4. 原型链
    JS基础知识_第1张图片

  5. instanceof
    判断引用类型属于哪个构造函数。

  6. 如何判断一个变量是数组烈性
    var arr = []
    arr instanceof Array //true

  7. 继承列子

        function Animal(){
            this.eat = function('naimal eat')
        }
        function Dog(){
            this.bark = function(){
                console.log('dog')
            }
        }
        Dog.prototype = new Animal()
        var hashiqi = new Dog()
  8. 描述new一个对象的过程

    • 创建一个新对象
    • this指向这个新对象
    • 执行代码,即对this赋值
    • 返回this
  9. zepto 源码中如何使用原型链

    • 阅读源码是提高技能的方式。
    • 不能埋头苦读 有技巧的在其中
  10. 示例

        function Elem(id){
            this.elem = ducument.getElementById(id)
        }
        Elem.prototype.html = function(val){
            var elem = this.elem
            if(val){
                elem.innerHTML = val
                return this
            }else {
                return elem.innerHTML
            }
        }
        Elem.prototype.on = function (type,fn){
            var elem = this.elem
            elem.addEventListener(type,fn)
        }
        var div1 = new Elem('div1'

你可能感兴趣的:(面试专题)