Several common js interview questions and js interview

Source: Internet
Author: User

Several common js interview questions and js interview
1. About the number type conversion in "+" and "-"

Var a = ''+ 3; // try to convert 3 to the string var B = 4; console. log (typeof a); console. log (a + B); console. log (a-B); // try to convert the string to numbervar foo = "11" + 2 + "1 "; // Add a string '1' and subtract a string '1. log (foo); console. log (typeof foo );

2. De-duplication of js Arrays

Easy to think

var arrayNum = [0, 0, 0, 1, 1, 3, 3, 4, 5, 6, 7];var arr = new Array();var  uniqArray = function (array) {    var newArray = [];    for (var i = 0; i < array.length; i++) {        if (newArray.length != 0) {            for (var m = 0; m < newArray.length; m++) {                var repeatNum = false;                if (array[i] == newArray[m]) {                    repeatNum = true;                    break;                }            }            if (repeatNum == false) {                newArray.push(array[i]);            }        }        else {            newArray.push(array[i]);        }    }    return newArray;};

The time complexity is O (n ^ 2)
Optimized Code:

Var arrayNum = [0, 0, 0, 1, 1, 3, 3, 4, 5, 6, 7]; var testArray = function (arry) {var tempArray = []; for (var I = 0, l = arry. length; I <l; I ++) {// use the indexof method of the array to determine whether to find the index of the current element in temparray if (tempArray. indexOf (arry [I]) ===- 1 & arry! = '') {TempArray. push (arry [I]) ;}} return tempArray;} console. log (testArray (arrayNum ));

The time complexity is O (n)

3. simple use of js objects

In js, there is no such keyword as class to declare an object. It is just a difference with js functions by capitalizing the first letter of the function variable name (js functions adopt the hump naming method)

Function DongJia (ver) {var defaultValue = 0.01; this. version = ver? Ver: defaultValue; this. getVersion = function () {return this. version;} this. setVersion = function (ver) {this. version = ver ;}} var dongjia = new DongJia (); // No Default console. log (dongjia. getVersion (); // use the set function to change the default value of dongjia. setVersion (0.02); console. log (dongjia. getVersion (); // if there is a default value, assign var dongjia2 = new DongJia (0.02) to the version directly. console. log (dongjia2.getVersion ());
4. clone the array in js

Copying objects using jquery

jQuery.extend( [ deep ], target , object1 [, objectN... ] )

Find the corresponding parameter based on the parameter name defined in the preceding syntax.

Parameter description
DeepOptional or Boolean indicates whether to merge objects in depth. The default value is false. If the value is true and a property of the same name of multiple objects is also an object, the attributes of this "property object" will also be merged.
TargetObject type. The member attributes of other objects are copied to the Object. Object1 (optional) the first Object to be merged of the Object type.
ObjectNOptional. N objects of the Object type to be merged.

Sample Code:

Var a = {k1: 1, k2: 2, k3: 3}; var B = {k4: 4, k5: 5}; var c; c = $. extend (a); // copy object a to the jquery object and assign it to the c console. log ('------------'); console. log (c ===$); // The c object points to the $ object, so the result is true console. log (a ===$); // false console. log ('------ c ------'); console. log (c. k2); // equivalent to $. k2 console. log ('------ c ------'); console. log (c); // c. k2 = 777; console. log ('------ a ------'); console. log (a); console. log ('------ B ------'); console. log (B); console. log ('------ $ ------'); console. log ($); console. log ($. k2 );

Result:

See the following code:

    var d = $.extend({}, a)    console.log(d);    d.k2 = 3456;    console.log(d);    console.log(a);

Result:

In jquery, extend () is not a copy reference, but a new object.

Note:
The object attributes of the function copy include methods. In addition, the property of the object inherited from the prototype will be copied (except for the built-in JS object ).
The default value of deep is false. You can specify true for this parameter, but not false. In short, the first parameter cannot be set to false.
If the parameter is null or undefined, the parameter is ignored.
If only $. extend () is specified, the target parameter is ignored. In this case, target is the jQuery object. In this way, we can add a new function for the Global Object jQuery.
If multiple objects have the same attributes, the latter will overwrite the attribute values of the former.

See the following code to copy an array object:

    var test = [1,2,34,];    console.log(test);    var contest= $.extend([],test);    console.log(contest);    contest.push(567);    console.log(test);    console.log(contest);

Result:

5. Use of js closure scenarios
<Ul> <li> you are my 1 </li> <li> you are my 2 </li> <li> You Are My 3 </li> <li> You Are My 4 </li> <li> You Are My 5 </li> </ul> <div id = "content"> </div>

When I click the li tag, the current li tag is removed and the content of the li tag is added to the div tag below.

The complete code is as follows:

<script type="text/javascript">    $(function() {        var $liObj = $('li'),            conText;        for (var i = 0, l = $liObj.length; i < l; i++) {            (function(i) {                $liObj.eq(i).on('click', function() {                    conText = $('#content').text();                    $('#content').html(conText + $liObj.eq(i).text());                    $liObj.eq(i).remove();                    return false;                });            })(i);        }    })    </script>

The required functions can be implemented after testing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.