Table of Contents
Basics of JavaScript [1]
2015/11/13 16:10:04
1. What is javascript?
2. JavaScript debugging
3. JavaScript test program
Home Backend Development PHP Tutorial JavaScript basics [1]_PHP tutorial

JavaScript basics [1]_PHP tutorial

Jul 12, 2016 am 09:04 AM
android

Basics of JavaScript [1]

2015/11/13 16:10:04

Javascript has been controversial since its inception, but this is still It does not affect its becoming the mainstream language for WEB programming today. The original JavaScript was designed to provide dynamic functions such as data interaction, screen rendering, and session authentication on the browser side. Now the popularity of node.js has extended JavaScript to the server side.

As a weakly typed scripting language, JavaScript’s syntax is not complicated. As programmers in this era, whether you are doing WEB development or not, it will be beneficial to be familiar with JavaScript. So today let’s get to know JavaScript neatly!

1. What is javascript?

Javascript is a dynamic scripting language, specially used for web application development. Its main function is to add dynamic behavioral effects to the page, specifically:

  • Embed dynamic text into HTML pages;
  • Respond to browser events (javascript is an event response language that can respond to user mouse clicks, movements, etc.);
  • Read and write HTML elements (such as form submission, etc.);
  • Verify data before submitting it to the server;
  • Detect the visitor's browser information;
  • Control cookies, including creation and modification;
  • Server-side programming based on Node.js technology;

It can be said that javascript is A literal scripting language that the client uses to program the dynamic behavior of HTML pages, making web browsers more than just displaying user pages. But precisely because JavaScript is deployed on the client, its security has always been the focus of attention.

2. JavaScript debugging

The execution of JavaScript scripts is mainly realized through the parsing engines independently developed by major browser manufacturers. The existing mainstream javascript parsing engines mainly include: Chrome's V8 engine, IE9's JS engine and Firefox's TraceMonkey;

For javascript development, we are often accustomed to having an IDE similar to VS that can be used directly, but Since javascript itself is a "lightweight" language, we only need a simple text editor javascript parsing engine for development and debugging.

Of course you can use Notepad under Windows or Vim editor on Linux, but I suggest that you use a dedicated code editor because it has many conveniences such as syntax highlighting and automatic completion. The text editor I use here is nodepad , and the accompanying debugger is the simple Firefox. Of course, you can also use IE, Chrome or even Safri, because nodepad supports multiple browser debugging.

Under the "Run" menu in Notepad, you can choose which browser to run the debugging javascript script, or you can use the above shortcut keys .

3. JavaScript test program

Next, we provide a very simple javascript script, and the knowledge points involved will be explained one by one.

First is our javascript script: program.js. Let’s look at a piece of code first:


<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>document.writeln('');<br /> </li><li>document.writeln("Hello, world!");<br /></li><li>var a = 100000000000000000000e400;<br /></li><li>if (a < Infinity)<br /></li><li>{<br /></li><li>document.writeln(a);<br /></li><li>document.writeln('a less than Infinity, 3Q~~');<br /></li><li>}<br /></li><li>else<br /></li><li>{<br /></li><li>document.writeln(a);<br /></li><li>document.write('Sorry, a more than Infinity!\n');<br /></li><li>} </li></ol>
Copy after login

Line 1: document.writeln() and document.write() are both used to output information to the terminal (browser). The difference is that the "ln" version has its own newline character;
Line 3: Define a variable, There is no need to declare its type, just use the keyword "var";
Line 4: Basic control structures can still be used in JavaScript, such as if-else, while and for, etc.; JavaScript numbers use 64 bits Floating point representation, so the value represented by 1.0 and 1 is the same; in addition, NaN represents an operation result that cannot produce a normal result, and all values ​​greater than 1.798e308 are uniformly represented by Infinity, and e308 represents 10 to the 308th power; so when input When the value is greater than the defined value, it is uniformly displayed as Infinity;


<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>var a = 10, b = 9;<br /> </li><li><br /></li><li>document.writeln(a);<br /></li><li><br /></li><li>function add(x,y)<br /></li><li>{<br /></li><li>return x + y;<br /></li><li>}<br /></li><li><br /></li><li>function subtract(x,y)<br /></li><li>{<br /></li><li>return x - y;<br /></li><li>}<br /></li><li><br /></li><li>document.writeln(add(a,b)); </li></ol>
Copy after login

A simple function is defined starting from line 5, one is addition (add) , the other is subtraction (subject), and then calls the function when the terminal outputs;


<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>document.writeln("Global Object...");<br /> </li><li>var MyObj = {};<br /></li><li><br /></li><li>MyObj.member = {'first-name': "Alice", last_name : "Winston"};<br /></li><li><br /></li><li>MyObj.record = {<br /></li><li>airline: 'T2B',<br /></li><li>number: 777,<br /></li><li>departure: {<br /></li><li>Date:"Sunday",<br /></li><li>Time:"2015-11-01",<br /></li><li>City:"Taiwan"<br /></li><li>},<br /></li><li>arrival: {<br /></li><li>Date:'Monday',<br /></li><li>Time:"2015-11-02",<br /></li><li>City:"Beijing"<br /></li><li>}<br /></li><li><br /></li><li>};<br /></li><li><br /></li><li>document.writeln("Retrive a non-exit attribute value ..exa..MyObj.people..");<br /></li><li>document.writeln(MyObj.people);<br /></li><li><br /></li><li>document.writeln("typeof MyObj.member is ...");<br /></li><li>document.writeln(typeof MyObj.member);<br /></li><li><br /></li><li>document.writeln("MyObj.record.number is ...");<br /></li><li>document.writeln(typeof MyObj.record.number);<br /></li><li><br /></li><li>document.writeln('MyObj.record.airline is ...');<br /></li><li>document.writeln(typeof MyObj.record.airline); </li></ol>
Copy after login

Since there is no local domain in the javascript function, The global domain is often used. Everyone with programming experience knows that global variables are very easy to mess up, so it is recommended to create a global object at the beginning like this article, and then all operations are part of it. In other words, the global object we define is actually a "global object". container".
The simple data types in JavaScript include numbers, strings, Boolean values, null values, and undefined. All other values ​​are objects, such as arrays, functions, and regular expressions. Simply put, objects in JavaScript are mutable keyed collections. Objects are composed of different attributes. The name of the attribute can be any string including the empty string, and the attribute value can be any value except undefined.

Line 2 initializes an empty global object MyObj;
Line 4 adds an attribute member to the object MyObj, and member is an object containing two key-value pairs. Each key-value pair uses ',' separated, the last attribute does not need to be added with a symbol;
The attribute name in lines 7-8 is a string. It is recommended to use the JavaScript identifier specification (numbers, letters and underscores, the first character can only be a letter) , because when writing this way, "" can be omitted to represent a string. For example, "first-name" cannot omit "", but can be written as first_name; not only that, you can conveniently use the reference symbol "." when retrieving the properties of the object. For example, MyObj.member.last_name, non-canonical identifiers can only use MyObj.member.["first-name"], which is very troublesome;

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>//对象属性值的更新<br /> </li><li>document.writeln('Attribute value update...');<br /></li><li>document.writeln(' Once Date is ' + typeof MyObj.record.departure.date);<br /></li><li><br /></li><li>MyObj.record.departure.Date = 'Saturday';<br /></li><li><br /></li><li>document.writeln(MyObj.record.departure.Date);<br /></li><li><br /></li><li>//对象枚举<br /></li><li>document.writeln('Object enume...')<br /></li><li>var name;<br /></li><li>for (name in MyObj.record)<br /></li><li>{<br /></li><li>document.writeln(name + ':' + MyObj.record[name]); </li></ol>
Copy after login

No. 3 Line uses typeof to obtain the type of the object. The typeof operator has only five values: "string", "boolen", "undefined", "function" and "object";
Line 5 directly updates the existing attributes. If it already exists, update it; if not, create the attribute key-value pair;
Line 9 shows enumerating all attributes of an object. With the for-in structure, we can enumerate all attributes (including Attributes in functions and prototypes), and the enumeration obtained is not necessarily in order, so it is generally recommended to use the form of for() to specify the traversal method;

Next is our program.html

<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li><strong style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;"><html><br />  </li><li><strong style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;"><body><br /> </li><li><strong style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;"><pre class="brush:php;toolbar:false"><br /> </li><li><strong style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;"> <br /> </li><li><strong style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;">
Copy after login


  • 这里关键就是第4行中嵌入了一个javascript脚本,
    第3行的告诉浏览器按照源码的样式显示;
    最后我们来看看结果:
    <strong style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;"> <img src="/static/imghw/default1.png"  data-src="http://www.bkjia.com/uploads/allimg/151116/1145394a9-1.png"  class="lazy" alt=""    style="max-width:90%"  style="max-width:90%" /> 不足之处欢迎大家批评指正!  Refer:《Javascript语言精粹》,Douglas Crockford, 电子工业出版社 <p><br /></p>
    Copy after login

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1071441.htmlTechArticlejavascript基础【一】 2015/11/13 16:10:04 javascript自诞生之初就处于争论之中,但是这依旧不影响其成为今天WEB编程的主流语言。最初的javascript设...
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

    In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

    Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

    OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

    IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

    Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

    Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

    The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

    Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

    Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

    New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

    In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

    Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

    The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

    iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship Sep 10, 2024 am 06:45 AM

    OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T

    See all articles