Web front-end development Beginners Ten Questions Collection (1)

Source: Internet
Author: User
Tags class definition script tag

Where can the 1.script label be placed

A: The script tag can be placed anywhere in the HTML file (any where), for example, can be placed outside the HTML tag, can also be placed inside the head, can also be placed inside the body, or can be placed in the meta. As shown below:
Outside of HTML tags:

<html><body></body></html><script type="text/javascript">    document.write(")</script>

Inside Head:

<html><head>    <meta charset="Utf-8"/>    <script type="Text/javascript">document.write (")</script> </head></meta><body></body></html>

Inside Body:

<html><head>    <meta charset="Utf-8"/></head></meta><body>    <script type="Text/javascript">document.write (")</script> </body></html>

Within Meta:

<html><head>    <meta charset="Utf-8">        <script type="Text/javascript">document.write (")</script>     <meta></head></meta><body></body></html>
What is the difference between where the 2.script label should be placed?

We know that the script tag can be placed anywhere in the HTML document. What position should be placed in the actual use?

difference: different locations, the difference is primarily the order in which JavaScript scripts load execution. The browser's parsing of the HTML file is loaded and parsed linearly from the top down, and <script> as part of the HTML code follows this principle, so the position is preceded by <script> execution.

write in HTML or separate into an external JS file: JavaScript code is placed in the HTML file or placed in a separate JS file adhere to the principle is: different HTML files shared by the JS script is placed in the JS file alone, Do not share them in the respective HTML files.

deciding where to place JavaScript scripts in an HTML file requires adherence to the following principles:
(1) Head tag is before the body tag processing, according to the Convention, no reference HTML elements of the JS script placed in the head tag;
(2) The JS script that references the HTML file element is placed between the label and the label </body> , or placed outside the label, which is the last side of the HTML file.

The JS script placed inside the head tag needs attention:
This means that you must wait until all JavaScript code has been downloaded, parsed, and executed before you can start rendering the contents of the page (the browser <body> starts rendering the content when it encounters the tag). For pages that require a lot of JavaScript code, this will undoubtedly cause the browser to have a noticeable delay in rendering the page, while the browser window in the delay period will be blank. The solution is to place the JS script after the HTML tag or the end of the body tag.

Does the JavaScript script within the 3.script tag execute when the page loads?

will be executed. JavaScript scripts defined within the HTML page and external scripts specified by the SRC attribute are executed. Because the JavaScript code on an HTML page is part of an HTML document, the order in which JavaScript is executed when the page is loaded is the order in which the markup is introduced <script> , which executes first. such as the following HTML and JS script files.

JS script file:

alert("已加载3");function load1(){    alert("已加载4");}

HTML file:

<! DOCTYPE html><html><head><meta charset="Utf-8" /><script type="Text/javascript">Alert ("page loaded 1!    ");     function load(){ alert ("page loaded 2"); }</script><script type="text/javascript" src="Test.js"></script></head><body onload="Load ()">    <H1>Hello world!</H1></body></html><script type="Text/javascript">Alert ("page loaded 5! ");</script>   

Open the HTML file in a browser, and then pop up: "Page loaded 1!" "," Loaded 3! "," Page loaded 5! "and" Page Loaded 2 ". The body's OnLoad event will not be triggered until the entire HTML file is loaded.

* * Note: The named function of **javascript (that is, a function with a name) is not executed when the page is loaded, and must be displayed before the call is executed. The onload event of the body above shows the call function load ().

Can global variables and functions defined by 4.Javascript scripts be called across a script tag?

Both global variables and functions can be called across a script tag.

But the difference between global variables and functions is that for global variables, whether they are in the same script or a different script, they must be defined before they are used. But for functions, the same script can be used first, then defined.

<! DOCTYPE html><html> <head> <meta charset="Utf-8"/> <script> var tmp=9;     </script> </head> <body> </body> <script>_alert (TMP); //Call function     function _alert(var1){ alert (var1); } _alert (TMP1); //Call function, popup undefined  var tmp1=8;         </script> </html>
What is the difference between null and "" Empty strings in 5.js?

In essence, NULL is an empty object, and its type is object. Alert (typeof (null)); Will output object;
"" is an empty string, alert (typeof ("")), and a string is output.

Using the difference, null can empty an object of any type, turning the object's type into object.

<script>    var str="lvlv";    str=null;    alert(str);    alert(typeof(str));  //输出"object"</script>

Eject separately: null and object.

What is the difference between null and undefined in 6.javascript?

Tell the difference between null and undefined before you describe what data types are in JS.

JS has 7 Big data types in W3cschool, namely: Undefined, Null, Boolean, number, String,array, and object.

However, it is important to note that there are no undefined and null keywords in JS. Undefined is a value of type undefined, and the undefined type is only undefined this value. Null is a value of the null type, and the null type has only one private value, NULL, which is its literal. Both null and undefined are values, not objects.

Why the TypeOf operator returns "Object" for null values. This is actually an error in the initial implementation of JavaScript, and is then used by ECMAScript.

There is also a need to note that:
The value undefined is actually derived from the value null, so ECMAScript defines them as equal.

alert(nullundefined);  //输出 "true"

So the difference between null and undefined is that NULL is a null-type value, and undefined is a value of type undefined.

A typeof operator is posted here to evaluate the return value of variables of various types:

The difference between 7.ECMAScript and JavaScript

ECMAScript is a script programming language that is standardized by the ECMA International (formerly the European Association of Computer manufacturers, the English name is European Computer Manufacturers Association) through ECMA-262.

ECMAScript is a core part of JavaScript, ECMAScript describes the syntax and basic objects of JavaScript;

In addition, the components of JavaScript include:
The DOM describes the methods and interfaces for handling Web page content;
The BOM describes the methods and interfaces that interact with the browser.

In other words, although ECMAScript is an important standard, it is not the only part of JavaScript, and of course, it is not the only one that is standardized. In fact, a complete JavaScript implementation is made up of the following 3 different parts:
Core (ECMAScript);
Document Object Model (DOM);
The browser object model (BOM).

The diagram is part of javascript:

8.JavaScript How to define a class

JavaScript itself does not support object-oriented, it does not have an access control, it does not define the class's keyword class, it does not support inherited extend or colons, it does not support polymorphic virtual functions and virtual keywords. However, JavaScript is a flexible language, so let's look at how JavaScript without the keyword class implements the class definition and creates the object.

How to define classes in JavaScript, create objects for classes, create public and private properties and methods, create static properties and methods, simulate constructors, and discuss error-prone this. Refer to: Defining classes in JavaScript. This article is well written and understandable, and is worth reading.

Can a method in 9.JavaScript have a return value?

The method in JS can have a return value. Because JS is a weakly typed language, you do not need to indicate the return value type for the method display, and return the return value directly using return, for example:

function add(a, b){    return a + b;}alert(add(1, 2));//3

If return is not written in the function, the function returns undefined and can be judged as to whether there is a return value.

What are the different representations of 10.html colors?

There are three main types,
The first type: RGB mode.
There are two ways to express it:
(1) Hexadecimal representation. such as red: #ff0000, Green: #00ff00, Blue: #0000ff. Note that hexadecimal is case-insensitive;

(2) Decimal or Percent form: red: rgb (255,0,255) or RGB (100%,0%,0%). Note RGB case is available;

If the alpha opacity is increased, the representation is as follows:
Red: Rgba (255,0,255,1) or Rgba (100%,0%,0%,1). The value range for Alpha opacity is 0 to 1 or it cannot be expressed as a percentage.

The second type: HSL mode.
This is the new color notation for CSS3. The HSL color model is also a color standard for industry, which corresponds to Hue (hue), also known as hue, saturation (saturation), lightness (lightness).

The values for each of the three are:
Hue (hue). 0 (or 360) indicates red, 120 is green, 240 is blue, and other values are preferred to specify the color. The value is: 0-360;
Saturation (saturation). The values are: 0%-100%;
Lightness (brightness). Values are: 0%-100%.

For example, Red is indicated as: HSL (0,%100,%50). If the brightness is 100%, then it becomes white. HSL is case insensitive.

If Alpha opacity is added, red can be represented as Hsla (0,%100,%50,1).

the third type: HTLM the predefined color names.
Use HTLM predefined color names, such as Red,blue,green.

Reference documents

[1] In the body write JavaScript will automatically execute?
[2]js load execution order in HTML
[3] Where should the JavaScript code be placed in the HTML code better?
[4]http://www.w3school.com.cn/js/pro_js_primitivetypes.asp
[5] JavaScript implementation
[6] Defining classes in JavaScript
[7]HSL Color

Web front-end development Beginners Ten Questions Collection (1)

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.