Interview questions JavaScript02【2021-10】

1. Please talk about your understanding of Ajax, how do you usually implement Ajax operations

        Ajax is a partial refresh technology that can send requests to the server for DOM manipulation

        accomplish:

                Native JS

                        XMLHttp (good compatibility)

                        fetch (provided by H5)

                script library

                        jQuery

                        axios

        Steps for native js:

//get
var xhr = new X MLHttpRequest();
xhr.open('GET',URL, true);
xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
        var res = JSON.parse(xhr.responseText);
        if(res.Code != 0){
            return ;
        }
        res.Data.forEach(function(item){
            ...
        }
    }
}
xhr.send();

//post
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

2. Tell me about your understanding of CORS, how to implement cross-domain requests?

        Understanding of CORS cross-domain issues-So say- Blog Park

        The question is the same-origin policy and cross-domain requests.

        CORS is a w3c standard, the full name is "Cross-Origin Resource Sharing". If one of the protocol, domain name, and port of a request Url is different from the address of the current page, it is cross-domain.

        img/audio/video/css/script are all cross-domain, only ajax requests are not cross-domain

        If the front end wants to solve the cross-domain problem, it needs to use nginx reverse proxy

3. Talk about your understanding of this

        In ordinary functions, this points to undefined in strict mode and window in non-strict mode

        In the constructor, this points to the instance itself

        In object methods, this points to the instance

        In the arrow function, this points to the parent execution environment (execution environment)

        Use call, apply, bind to change the direction of this

4. How to judge whether an object belongs to a certain type?

        1-typeof is only suitable for basic data types

        2-instanceof can judge the object data type, based on the prototype chain

        3-constructor points to the constructor where the prototype object is located

        4- Object.prototype.toString.apply() built-in

5. Talk about your understanding of json and xml

        Common point: Function: Realize data transfer between different platforms, languages, and systems

        XML: Extensible Markup Language, which describes data structures and relationships through custom tags

        It looks very redundant and difficult to parse

<persons>
    <person>
        <id>1</id>
        <name>Rose</name>
    </person>
    <person>
		<id>2</id>
		<name>Jack</name>
	</person>
</persons>

JSON: very fast parsing JSON.stringify|parse

persons =[
		{id:1,name:'Rose'},
		{id:2,name:'Jack'}
	]

6. What is the point of JavaScript code contained in a self-executing function block? Why do you want to do this?

        What is the purpose of self-executing functions in javascript? _Mooc Ape Ask

        A self-invoking function is a function that is executed immediately when it is defined

        It keeps code to a minimum, enforces separation of behavior from expression, provides a closure that prevents naming collisions, enables encapsulation, allows all .js files to be included in self-executing functions, and prevents global namespace pollution

7. What is the event flow model of Javascript?

        Understanding Event Handling in JavaScript Develop Paper

        Event bubbling model first child then parent (default)

        Event capture model first father then son

        true and false in addEventListener

Eight, several methods of event binding

        Inline, event attribute, addEventListener

        xxx.onclick = function(){} One handler of one event source can only bind one method, if there are too many, it will be overwritten

        addEventListener A handler of an event source can bind multiple methods, and the third parameter true|false can indeed be bubbling or capturing

                                                Can be removed with removeEventL

                                                Disadvantages: poor compatibility, low version of IE does not support, but can be replaced by attachEvent

9. What is event bubbling, how to prevent event bubbling and event default behavior

        Event bubbling, first son and then father

        JS prevents bubbling and cancels default events (default behavior) - front-end development blog

        Stop bubbling and capturing: event.stopPropagation()

        Cancel the default behavior: event.preventDefault()

        return false

10. Talk about your understanding of event agency and how you realized it

        Understanding of JS event proxy - short book

        Event proxy is implemented by using event bubbling to improve performance

        Implementation 1: onclick + e.target

        Implementation 2: addEventListener

11. What is the difference between the load event of the window and the DomContentLoaded of the document?

        load is triggered after all resources are loaded, DOMContentLoaded is triggered after the DOM tree is loaded

12. Write the output of the following code

        1 + "2" + "2"                122

        1 ++ "2" + "2"` reports an error 

        1 +- "1" + "2"                02

        + "1" + "1" + "2"            112

        "A" - "B" + "2"               NaN2

        "A" - "B" + 2                  NaN

13. The output of the following code is

        0.1 + 0.2                   0.30000000000000004

        0.1 + 0.2 == 0.3        false

14. Please write the code so that the expression a==1 && a == 2 returns true

        higher order functions

        How to implement a===1 && a===2 && a===3 to return true for interview questions

        Is it possible for JavaScript (a ==1 && a== 2 && a==3) to be true? _Front-end kit blog-CSDN blog

        JavaScript: (a==1 && a==2 && a==3) can output ture? _weixin_33946605's Blog - CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_43961652/article/details/121218531