JavaScript 面试题 1

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

  • Library like jQuery gives us a neat toolkit to help with implementing some functions.
  • Frameworks provides us with an entire project architecture to follow in order to build a well structured javascript project.

4. Json and Jsonp difference

  • JSONP is a method for sending JSON data without worrying about cross-domain issues.

5. What is an anonymous fuction

  • An anonymous function is a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its intial creation. The most commom use for anonymous functions are as arguments to other functions, or as a closure.

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?

  • An object method is a function definition, stored as a property value

9. What is JavaScript Scope?

  • In JavaScript, scope is the set of variables, objects, and functions you have access to.
  • local scope: can only be accessed within the function
  • global scope: all scripts and functions on web page can access it

10. The lifetime of JavaScript variables

  • Starts when the variable declared
  • Local variable ends when the function is completed.
  • Global variables are deleted when you close the page.

11. What is Closure

  • A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).
  • Private variables can be made possible with closures. The nested function can access the variable in the parent scope. This is called a JavaScript closure. It makes it possible for a function to have private variables.

12. What is HTML DOM?

  • HTML DOM is standard for how to get, change, add, or delete HTML elements. With the HTML DOM, javascript can access and interact with all the elements of an HTML document.

13. addEventListener() method:

  • It attaches an event handler to the specified element. It can assign different function to a specified element.

14. What is XMLHttpRequest object used for?

  • It exchanges data with a server behind the scenes.
  • You can update a web page without reloading the page, request data from a serer after the page has loaded, receive data from a server after the page has loaded, send data to a server in the background.

15. Difference of setTimeout and setInterval

  • Settimeout calls a function or evaluates an expression after a specified number of milliseconds.
  • SetInterval calls a function or evaluates an expression at time intervals in milliseconds. and will continue calling the function until clearInterval is called, or the window is closed.

16. Difference between overloading and overriding

  • Overloading deals with the notion of having two or more methods in the same class with the same name but different arguments.
  • Overriding means having two methods with the same argument, but different implementation. One of them would exist in the Parent class while another will be the derived class.

17. Explain this keyword

  • In JavaScript, this will always refer to the owner of the function we are executing or as an object method

18. What are browser detection and feature detection

  • Browser detection and feature detection are two most common ways to test whether the technologies you use in your application can apply to the current browser or not. Browser detection mainly check to match the browser type strings, while the feature detections are consist of small scripts to test whether the feature you need is supported by the current browser or not(this is not about the version or the browser type)

  • Browser Detection:
    It is one approach to cover browser differences. The most common approach is to use JavaScript to query the user-agent header:

    <script type="text/javascript">
        if(navigator.userAgnet.indexof("MSIE")>0){
            //run custom code for internet explorer
        }
    </script> 
    
  • Usually use it when dealing with the legacy applications.
    Disadvantages:
    (1). Should track which features the browser supports in detail, one wrong assumption could break the site.
    (2). Does not consider the version of the browser, it is not future-proof.

  • Feature Detection:

    • Better approach to handle differences in web browsers in web pages
    • Before using a feature that you know has different implementations in the various browsers, you can run a small set of tests where you look for the availability of a specific object, method, property, or behavior.

    In most cases, this can be done by trying to create a new instance of the feature in question. If that instantiation returns something other than null, then executing browser knows the feature. If not, you can test to tsee if there's a workaround or propritary legacy implementation of the feature available.

    Two important recommendations for feature detection:
    (1). test for standards, as a browser often supports the newer standard as well as the legacy work around
    (2). only target related features in a single check , to minimize assumptions about a browser's capability

20. Difference between apply and call

  • Apply lets you invoke the function with arguments as an array, call requires the parameters be listed explicitly.
  • Why should we use apply or call? Sometimes we want to borrow some functions from other object, we can use the function and call it with arguments from our own properties.

21. Singleton:

  • It limits the number of instances of a particular object to just one. It is implemented as an immediate anonymous function, the Module pattern is JavaScript's manifestation of it. Ensure all object access the single instance

    var Singleton = (function(){
        var instance;
        
        function createInstance(){
            var object = new Object("I am instance");
            return object;
        };
        
        return { getInstance: function(){
            if(!instance){
                instance = createInstance();
            }
            return instance;
        }};
    })();
    
    function run(){
      var instance1 = Singleton.getInstance();
      var instance2 = Singleton.getInstance();
      alert("Same instance? " + (instance1 === instance2));  
    }
    

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

  • Immediate functions execute as soon as JavaScript encounters them. Creating an immediate function: add the open/close parentheses after the closing curly bracket, and then wrap the entire function in parenthese.
  • Self invoke 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

  • Every JS object has a prototype. The prototype is also an object. The prototype property allows you to add new properties and methods to existing object. All JavaScript objects inherit their properties and methods from their prototype called Object.prototype. The standard way of creating a prototype is through the constructor function.
  • In JavaScript you first create an object, then you can add objects or create new objects from it.
  • Example:
//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?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,355评论 0 23
  • 程序可以分为两种:「非事件驱动」型程序、「事件驱动(event-driven)」型程序。 • 非事件驱动型程序 这...
    IvanHung阅读 908评论 0 1
  • 1、 虚拟机栈:每个线程有一个私有的栈,随着线程的创建而创建。栈里面存着的是一种叫“栈帧”的东西,每个方法会创建一...
    Firmbelief阅读 490评论 0 1
  • 我爸爸走得早,我17岁时就因为疾病去世了。那时的我性格极其内向,我爸爸对此很是忧心,弥留之际还在念叨,你这个性...
    小小的心思阅读 164评论 1 1
  • 我曾经的睡眠状况 失眠是始自高三开始严重的,那时沉重的学业压力和第一次的宿舍生活让我整晚整晚的睡不着,严重时甚至喘...
    查无此蜗阅读 851评论 0 14