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?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末坐儿,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子译红,更是在濱河造成了極大的恐慌品嚣,老刑警劉巖唾那,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拼弃,死亡現(xiàn)場離奇詭異线衫,居然都是意外死亡啡氢,警方通過查閱死者的電腦和手機(jī)菩暗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門掰曾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人停团,你說我怎么就攤上這事旷坦。” “怎么了佑稠?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵秒梅,是天一觀的道長。 經(jīng)常有香客問我舌胶,道長捆蜀,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任幔嫂,我火速辦了婚禮辆它,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘履恩。我一直安慰自己锰茉,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布切心。 她就那樣靜靜地躺著飒筑,像睡著了一般。 火紅的嫁衣襯著肌膚如雪绽昏。 梳的紋絲不亂的頭發(fā)上协屡,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音全谤,去河邊找鬼肤晓。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的材原。 我是一名探鬼主播沸久,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼季眷,長吁一口氣:“原來是場噩夢啊……” “哼余蟹!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起子刮,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤威酒,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后挺峡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體葵孤,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年橱赠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了尤仍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡狭姨,死狀恐怖宰啦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情饼拍,我是刑警寧澤赡模,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站师抄,受9級(jí)特大地震影響漓柑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜叨吮,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一辆布、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧茶鉴,春花似錦谚殊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至围肥,卻和暖如春剿干,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背穆刻。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來泰國打工置尔, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人氢伟。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓榜轿,卻偏偏與公主長得像幽歼,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子谬盐,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,495評(píng)論 0 23
  • 程序可以分為兩種:「非事件驅(qū)動(dòng)」型程序甸私、「事件驅(qū)動(dòng)(event-driven)」型程序。 ? 非事件驅(qū)動(dòng)型程序 這...
    IvanHung閱讀 928評(píng)論 0 1
  • 1飞傀、 虛擬機(jī)棧:每個(gè)線程有一個(gè)私有的棧皇型,隨著線程的創(chuàng)建而創(chuàng)建。棧里面存著的是一種叫“棧幀”的東西砸烦,每個(gè)方法會(huì)創(chuàng)建一...
    Firmbelief閱讀 499評(píng)論 0 1
  • 我爸爸走得早幢痘,我17歲時(shí)就因?yàn)榧膊∪ナ懒嘶8瘛D菚r(shí)的我性格極其內(nèi)向,我爸爸對(duì)此很是憂心颜说,彌留之際還在念叨购岗,你這個(gè)性...
    小小的心思閱讀 175評(píng)論 1 1
  • 我曾經(jīng)的睡眠狀況 失眠是始自高三開始嚴(yán)重的,那時(shí)沉重的學(xué)業(yè)壓力和第一次的宿舍生活讓我整晚整晚的睡不著脑沿,嚴(yán)重時(shí)甚至喘...
    查無此蝸閱讀 864評(píng)論 0 14