Common JavaScript Technical points

Source: Internet
Author: User

1. JavaScript Object-oriented inheritance implementation

JavaScript's object-oriented inheritance implementations generally use constructors and prototype prototype chains, with the following simple code:

[JavaScript]View Plaincopyprint?
  1. <span style="Font-family:microsoft Yahei; font-size:12px "> function Animal (name) {
  2. this.name = name;
  3. }
  4. Animal.prototype.getName = function () {
  5. Alert (this.name)
  6. }
  7. function Dog () {};
  8. Dog.prototype = New Animal ("Buddy");
  9. Dog.prototype.constructor = Dog;
  10. var dog = new Dog ();</span>
2. Write out 3 typical applications using this (1) in the HTML element event properties, such as [JavaScript]View Plaincopyprint?
    1. <span style="Font-family:microsoft Yahei; font-size:12px "><input type=" button "onclick=" Showinfo (this); "Value=" click "/></span>
(2) Constructors [JavaScript]View Plaincopyprint?
    1. <span style="Font-family:microsoft Yahei; font-size:12px ">function Animal (name, color) {
    2. this.name = name;
    3. this.color = color;
    4. }</span>
(3) [JavaScript]View Plaincopyprint?
    1. <span style="Font-family:microsoft Yahei; font-size:12px "><input type=" button "id=" text "value=" click "/>
    2. <script type="Text/javascript" >
    3. var btn = document.getElementById ("text");
    4. Btn.onclick = function () {
    5. Alert (this.value); //The This here is a BUTTON element
    6. }
    7. </script></span>
(4) Use the This keyword in CSS expression expressions [JavaScript]View Plaincopyprint?
    1. <span style="Font-family:microsoft Yahei; font-size:12px "><table width=" 100px "height=" 100px ">
    2. <tr>
    3. <td>
    4. <div style="width:expression (this.parentNode.width);" >div element</div>
    5. </td>
    6. </tr>
    7. </table></span>
3. How to deeply clone an object in JavaScript [JavaScript]View Plaincopyprint?
  1. <span style="Font-family:microsoft Yahei; font-size:12px ">function Cloneobject (o) {
  2. if (!o | | ' object '!== typeof o) {
  3. return o;
  4. }
  5. var c = ' function ' = = = = typeof o.pop?  [] : {};
  6. var p, v;
  7. For (p in o) {
  8. if (O.hasownproperty (p)) {
  9. v = o[p];
  10. if (v && ' object ' = = = = typeof v) {
  11. C[P] = Ext.ux.clone (v);
  12. } Else {
  13. C[p] = v;
  14. }
  15. }
  16. }
  17. return C;
  18. };</span>
4. What is Ajax? An interactive model of Ajax? What is the difference between synchronous and asynchronous? How do I troubleshoot cross-domain issues?

Ajax is a browser and server interaction technology that combines a variety of technologies, and the basic idea is to allow an Internet browser to make asynchronous HTTP calls to a remote page/service and update a current Web page with the received data without having to refresh the entire page. This technology can improve the client experience. Technologies included:
XHTML: The XHTML specification that corresponds to the XHTML1.0 is now a.
CSS: corresponding to the CSS specification, is currently CSS2.0
DOM: The DOM here mainly refers to the HTML Dom,xml DOM included in the following XML
JavaScript: ECMAScript specification corresponding to the ECMA
XML: Specification of XML DOM, XSLT, XPath, etc.
XMLHttpRequest: Web Applications1.0 Specification (http://whatwg.org/specs/web-apps/current-work/) corresponding to WHATWG

Ajax Interaction Model
Synchronization: The script will stay and wait for the server to send a reply before continuing
Async: Script allows a page to continue its process and handle possible replies

Cross-domain problem simple understanding is because of the limitations of the JS homologous policy, a.com domain name JS can not operate B.Com or c.a.com under the object, the specific scenario is as follows:
PS: (1) If the cross-domain problem is caused by a port or protocol the front end is powerless

(2) on a cross-domain issue, the domain is only identified by the header of the URL and does not attempt to determine whether the same IP address corresponds to the domain or two domains corresponding to an IP
Front-end for cross-domain workarounds:
(1) Document.domain+iframe
(2) Dynamically create a script tag

5. What is a closure? Below this UL, how to click on each column when alert its index? [HTML]View Plaincopyprint?
  1. <span style="Font-family:microsoft Yahei; font-size:12px "><ul id=" Test ">
  2. <Li> This is the first </li>
  3. <Li> This is the second </li>
  4. <Li> This is the third </li>
  5. </ul></span>


A closure is generated when an intrinsic function is called by the outer region of the function that defines it.

[JavaScript]View Plaincopyprint?
  1. <span style="Font-family:microsoft Yahei; Font-size:12px "> (function A () {
  2. var index = 0;
  3. var ul = document.getElementById ("test");
  4. var obj = {};
  5. for (var i = 0, L = ul.childNodes.length; i < L; i++) {
  6. if (ul.childnodes[i].nodename.tolowercase () = = "Li") {
  7. var li = ul.childnodes[i];
  8. Li.onclick = function () {
  9. index++;
  10. alert (index);
  11. }
  12. }
  13. }
  14. }) ();</span>
6, please give the asynchronous loading JS scheme, not less than two

By default JavaScript is loaded synchronously, that is, JavaScript loading is blocked, after the elements to wait for the JavaScript to be loaded before loading, for some meaning is not very large javascript, if placed in the page header will cause the load is very slow , it can seriously affect the user experience.
Asynchronous Load Mode:
(1) Defer, only IE support
(2) Async:
(3) Create script, insert into DOM, callback after loading, see code:

[JavaScript]View Plaincopyprint?
  1. <span style="Font-family:microsoft Yahei; font-size:12px ">function loadscript (URL, callback) {
  2. var script = document.createelement ("script")
  3. Script.type = "Text/javascript";
  4. if (script.readystate) { //ie
  5. Script.onreadystatechange = function () {
  6. if (script.readystate = = "Loaded" | |
  7. Script.readystate = = "complete") {
  8. Script.onreadystatechange = null;
  9. Callback ();
  10. }
  11. };
  12. } else { //others:firefox, Safari, Chrome, and Opera
  13. Script.onload = function () {
  14. Callback ();
  15. };
  16. }
  17. script.src = URL;
  18. Document.body.appendChild (script);
  19. }</span>
7, please design a set of programs to ensure that the page JS loaded completely. [JavaScript]View Plaincopyprint?
  1. <span style="Font-family:microsoft Yahei;  font-size:12px ">var n = document.createelement (" script ");
  2. N.type = "Text/javascript";
  3. Omit part of the code above
  4. IE supports script's ReadyStateChange attribute (ie support the ReadyStateChange event for script and CSS nodes)
  5. if (ua.ie) {
  6. N.onreadystatechange = function () {
  7. var rs = this.readystate;
  8. if (' loaded ' = = = = = RS | | ' complete ' = = = rs) {  
  9. N.onreadystatechange = null;
  10. F (ID, url); //callback function
  11. }
  12. };
  13. //Omit part of the code
  14. //safari 3.x supports the Load event for script nodes (DOM2)
  15. N.addeventlistener (' load ', function () {
  16. F (ID, url);
  17. });
  18. //firefox and Opera support onload (but not dom2 in FF) handlers for
  19. //script Nodes Opera, but no FF, support the OnLoad event for link
  20. //nodes.
  21. } Else {
  22. N.onload = function () {
  23. F (ID, url);
  24. };
  25. }</span>
8. How to define class in JS, how to extend prototype?

Ele.classname = "* * *"; defined in CSS in the following form:. * * * {...}
a.prototype.b = C;
A is the name of a constructor
B is the property of this constructor
C is the value of the property you want to define

9. The difference between Documen.write and innerHTML

Document.Write can only redraw the entire page
innerHTML can redraw part of a page

10. Optimization problem of front-end development

(1) Reduce the number of HTTP requests: CSS Spirit,data URI
(2) Js,css source compression
(3) Front-end template js+ data, reduce the bandwidth wasted due to HTML tags, front-end with variables to save AJAX request results, each operation of local variables, no request, reduce the number of requests
(4) Use innerHTML instead of DOM operation, reduce DOM operation times, optimize JavaScript performance
(5) Use settimeout to avoid page loss response
(6) using hash-table to optimize the search
(7) Set classname instead of directly manipulating style when you need to set a lot of styles
(8) Use less global variables
(9) The result of caching DOM node lookups
(10) Avoid using CSS Expression
(11) Picture Pre-load
(12) Avoid using table,table in the main layout of the page to wait for the content to be fully downloaded before it appears, showing slower than the div+css layout

Common JavaScript Technical points

Related Article

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.