Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

面试题(javascript) #31

Open
Myoursky opened this issue Feb 27, 2018 · 0 comments
Open

面试题(javascript) #31

Myoursky opened this issue Feb 27, 2018 · 0 comments

Comments

@Myoursky
Copy link
Owner

Myoursky commented Feb 27, 2018

1. 继承与原型链

2. javascript事件绑定方式

  • 直接在元素上增加事件。
  • 获取元素对象添加事件。
  • 通过addEventListener添加事件。IE9以下通过attachEvent添加。
  <input type="button" onclick="alert('按钮被点击1')" value="按钮1" />
  <input type="button" value="按钮2" id="button2" />
  <input type="button" value="按钮3" id="button3" />
  <script>
    function addEvent(obj,type,handle){
      try{  // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
          obj.addEventListener(type,handle,false);
      }catch(e){
          try{  // IE8.0及其以下版本
              obj.attachEvent('on' + type,handle);
          }catch(e){  // 早期浏览器
              obj['on' + type] = handle;
          }
      }
    }
    var button2 = document.getElementById('button2');
    button2.onclick = function() {
      alert('按钮被点击2');
    }
    var button3 = document.getElementById('button3');
    addEvent(button3,'click',function(){
      alert('按钮被点击3');
    })
  </script>

3. 跨域解决方案

  • JSONP
  • WebSocket
  • CROS
  • 自己搭建代理服务器(node, nginx)

4. var let和const的区别

  • let是升级版的var,增加了块级作用域的概念。
  • let声明的全局变量不是全局对象的属性,即不能通过window.来访问。
  • 形如for (let x...)的循环在每次迭代时都为x创建新的绑定。
  • let生命变量声明前使用变量会报错(ReferenceError),var不会报错,但是取不到值。
  • let不能重复生命变量
  • const声明的变量只可以在声明时赋值,不可随意修改,否则会导致SyntaxError。
    参考文章

5. 箭头函数和普通函数的区别

  • 箭头函数没有自己的this变量,它的this对象都是继承自外围作用域。
  • 箭头函数没有arguments对象,在ES6中,你可能更多地会使用不定参数和默认参数值这些新特性。
    参考文章

6. class怎么实现私有属性和方法

  • class没有提供private实现私有属性和方法
  • 可以利用Symbol值的唯一性来实现
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant