JavaScript 面试题 1

2018-01-04  本文已影响0人  咕嘿嘿DAZE

1. Difference between == and ===

==: compare value
===: compare value and type

2. Speed up web:

+ minificaiton, less downloading, ajax, css in the front, javascript on the end,

3. Difference between JavaScript Framework and Library

4. Json and Jsonp difference

5. What is an anonymous fuction

6. what is the type of variable without a value:

undefined

7. Set a variable to empty:

set it as null

8. How to define and call a function within an object?

9. What is JavaScript Scope?

10. The lifetime of JavaScript variables

11. What is Closure

12. What is HTML DOM?

13. addEventListener() method:

14. What is XMLHttpRequest object used for?

15. Difference of setTimeout and setInterval

16. Difference between overloading and overriding

17. Explain this keyword

18. What are browser detection and feature detection

20. Difference between apply and call

21. Singleton:

22. How to catch unhandled JavaScript errors?

Assign the window.onerror event to event handler.

<img src="image.gif" onerror="myFunction()>
<script>
    function myFunction(){
        alert('The image could not be loaded.');
    }
</script>

23. When does the window.onerror event fire?

In a nutshell, the event is raised when either:
1) there is an uncaught exception
2) a compile time error occurs
Uncaught exceptions:
throw some messages, call something undefined, security exception, compile error

24. What is immediate function

25. Strict Mode?

strict mode delete some JavaScript silent errors by changing them to throw erros.
strict mode code can sometimes be made to run fast than identical code that is not strict mode.
strict mode is declared by adding "use strict;" to the beginning of the JS file or JS function.
declared at the beginning of a JS file, it has glogbal scope, declared in a function it has local scope.
  1. How to reload page with JavaScript
    location.reload(forceGet);
    forceGet:
    + true-page must reloaded from the server;
    + false(default)-page reloaded from the cache

  2. Minification
    In JS, it means removing all unnecessary characters, such as spaces, new lines, comments without affecting the functionality of the source code.

  3. document.write()
    The write method writes HTML expressions or JS code to a document.

  4. What is event bubbling and capturing?Difference?Which one is better or faster
    Two ways of event propagation in the HTML DOM, bubbling and capturing
    Event propagation is a way of defining the element order when an event occurs. If you have a <p> element inside a <div>, the user clicks on <p>, which element's click event should be handled first? In bubbling the innermost element's event is handled first and then the outer; In capturing, the outermost element's event is handled first and then the inner.
    With the addEventListener method you can specify the propagation type by using the useCapture parameter:
    addEventListener(event,function,useCapture);
    The default value is babbling, when the useCapture is set to true, the event uses the capturing propagation. Only event bubbling model is supported by all the major browsers.

  5. JavaScript Hoisting
    In JS, a variable can be declared after it has been used. JS initialization are not hoisted. It is better to declare the variables on the top.

  6. Namespace
    It is used for grouping the desired functions, variables, under a unique name. It is a name that has been attached to the desired functions, objects and properties. This improves modularity in the coding and enables code reuse.
    In JavaScript a namespace is just another object containing methods, properties and objects.
    JS has a big design flaw, where it is very easy to create global variables that have the potential to interact in negative ways. The practice of namespacing is usually to create an object literal encapsulating your own functions and variables, so as not to collide with those created by other libraries:
    var MyApplication = {
    var1: val1,
    myfun: function(){

    },
    ...
    

    };
    Instead of calling myfun() globally, it would always be called as: MyApplication.myfun(). It is then less likely for our method or function to collide with other libraries.

32. Prototype in JS

//Define a functional object to hold persons in Js
var Person = function(name){
    this.name = name;
};

//Add dynamically to the already defined object a new getter
Person.prototype.getName = function(){
    return this.name;
}

//Create a new object of type Person
var john = new Person("John");

alert(john.getName());

//If now I modify Person object, also john gets the updates
Person.prototype.sayMyName = function(){
    alert('Hello, my name is '+ this.getName());
}

john.sayMyName();
  1. Optimize JavaScript code:

    1. Use local function variables instead of global variables
      Global variables lives in a highly-populated namespace, browser must distinguish between global variables and properties of objects that are in the current context
    2. Avoid adding short strings to long strings
      It needs to duplicate the long string
    3. Avoid pitfalls with closures
      Colusres are a powerful and useful feature of JavaScript, however, they have several drawbacks, including:
      they are the most common source of memory leaks; creating a closure is significatly slower than creating an inner function without a closure, and much slower than reusing a static function.
    4. Use hoisting carefully
  2. JS testing:
    Unit testing: a software testing method by which individual part of source code get tested.

    End to end testing: test whether the flow of an application from start to finish is behaving as expected, the
    purpose of performing it is to identify system dependencies and to ensure that the data integrity is maintained between various systems componenets and systems.

    The entire application is tested for critical functionalities such as communication with the other systems, interfaces, database, network and other applications.

    Jasmine is a behavior-driven development framework for testing JS code. It does not depend on any other JS frameworks. It does not require a DOM, it has a clean, obvious syntax so that you can easily write tests.

    A test suite begins with a call to the global Jasime function named describe with two parameters: a string and a function. The string is a name or title for a spec suite. The function is a block of code that implements the suite.

  3. What are JavaScript types:
    Number, string, boolean, function, object, null, undefined

  4. What is the use of isNaN function
    it returns true if the argument is not a number, otherwise false.

  5. Explain how can you submit a form using JavaScript
    document.form[0].submit();

  6. Disable the enter key on HTML form:
    $(document).keypress(
    function(event){
    if(event.which == '13') {
    event.preventDefault();
    }
    });
    //or this way
    $('input').on('keydown', function(event) {
    var x = event.which;
    if (x === 13) {
    event.preventDefault();
    }
    });

  7. Type of null and type of undefined:
    undefined means the variable has been decalred, but no value has been assigned to the variable
    null means the variable has been assigned a value as a representation of no value

  8. Use a object literal:
    A easy way to create JS object, you both define and create an object in one statment.
    An object literal is a list of name: valule pairs inside curly braces.

    use the new keyword:
    var person = new Object();
    person.firstname = "John";

    use an object constructor
    If a object type is needed, the standard way to create it is to use an object constructor function:
    function person(first, last){
    this.first = first;
    this.last = last;
    }
    var girl = new person("a","b");

  9. Inheritence in JavaScript
    JavaScript is a class-free, oo language, so it uses prototypal inheritance insted of classsical inheritance. Inheritance helps us better organize our code and reuse some code. Basically, there are three types of Inheritence in JS:
    1). Pseudoclassical pattern
    It tries to point a child's prototype to a parent's prototype.
    child.prototype = parent.prototype;
    2). Function pattern
    It allows one object to inherit from another, take the result and augment it at the child level to achieve inheritance.
    function Human(){...return human}
    var david = Human();
    There is no need for us to use new keyword, constructors, it just directly passes you the parent object. It has downside for performance because each object is unique, meaning each function call creates a new object, so the javascript interpreter has to assign new memory to thte function in order tot recompile everthing inside of it as unique again.
    3). Prototypal pattern
    It directly clones
    var male = Object.create(human);

42.What is event delegation?

Event delegation refers to the process of using event propagation (bubbling) to handle events at a higher level in the DOM than the element on which the event originated. It allows us to attach a single event listener for elements that exist now or in the future. Inside the Event Handling Function

43.the difference of scope and context

44.how to dynamic change the context?

上一篇下一篇

猜你喜欢

热点阅读