


A complete collection of unknown popular basic JavaScript knowledge (Collection)
Learning JavaScript is very boring and a headache. This article will help you deepen your impression of the basic knowledge. You will use it in your future study. Save it if you need it.
1. JS built-in objects
(1)Number
Creation method:
var myNum=new Number(value); var myNum=Number(value);
Properties and methods:
toString(): Convert to string
using use with using using through through off out out through out out through out out through out out together out right out right out through off out off ’ through ’ through ‐ through ‐ through ‐ through through through through through through through‐‐‐ to toString(): to be converted into a string.
Valueof (): Return to the basic value of a Boolean object (Boolean)
(3) String
var bool = new Boolean(value); var bool = Boolean(value);
Properties and Methods:
LENGTH: The length of the strings
charAt(): Returns the index character
charCodeAt: Returns the index character unicode indexOf(): Returns the index of the character
lastIndexOf();Reversely returns the index of the character
split(); The string is cut into an array based on special characters
# toUpperCase(); : Put all elements of the array into a string. The element is separated by the specified separator of
POP (): deletes and returns the final element
Push (): Add one or more elements to the end of the array, and return the new length
reverse () ;Reverse array
use using ’ ‐ ’ s ’ ’ s ‐ ‐ ‐ ‐ ‐ ‐ sort(); to
var myDate = new Date(); var myDate = new Date(毫秒值);//代表从1970-1-1到现在的一个毫秒值
属性和方法
getFullYear():年
getMonth():月 0-11
getDate():日 1-31
getDay():星期 0-6
getTime():返回1970年1月1日午夜到指定日期(字符串)的毫秒数
toLocalString();获得本地时间格式的字符串
(6)Math
创建方式:
Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(),像 Math.sin() 这样的函数只是函数,
不是某个对象的方法。您无需创建它,通过把 Math 作为对象使用就可以调用其所有属性和方法。
属性和方法
PI:圆周率
abs():绝对值
ceil():对数进行上舍入
floor():对数进行下舍入
pow(x,y):返回 x 的 y 次幂
random():0-1之间的随机数
round():四舍五入
(7)RegExp
创建方式:
var reg = new RegExp(pattern);
var reg = /^正则规则$/;
规则的写法:
[0-9]
[A-Z]
[a-z]
[A-z]
\d 代表数据
\D 非数字
\w 查找单词字符
\W 查找非单词字符
\s 查找空白字符
\S 查找非空白字符
n+ 出现至少一次
n* 出现0次或多次
n? 出现0次或1次
{5} 出现5
{2,8} 2到8次
方法:
test(str):检索字符串中指定的值。返回 true 或 false
需求:
校验邮箱:
var email = haohao_827@163.com var reg = /^[A-z]+[A-z0-9_-]*\@[A-z0-9]+\.[A-z]+$/; reg.test(email);
二、js的函数
1、js函数定义的方式
(1)普通方式
语法:function 函数名(参数列表){函数体}
示例:
function method(){
alert("xxx");
}
method();
(2)匿名函数
语法:function(参数列表){函数体}
示例:
var method = function(){
alert("yyy");
};
method();
(3)对象函数
语法:new Function(参数1,参数2,...,函数体);
注意:参数名称必须使用字符串形式、最后一个默认是函数体且函数体需要字符串形式
示例:
var fn = new Function("a","b","alert(a+b)");
fn(2,5);
2、函数的参数
(1)形参没有var去修饰
(2)形参和实参个数不一定相等
(3)arguments对象 是个数组 会将传递的实参进行封装
function fn(a,b,c){
//var sum = a+b+c;
//alert(sum);
//arguments是个数组 会将传递的实参进行封装
for(var i=0;i
}
}
fn(1,2,4,8);
3、返回值
(1)在定义函数的时候不必表明是否具有返回值
(2)返回值仅仅通过return关键字就可以了 return后的代码不执行
function fn(a,b){
return a+b;
//alert("xxxx");
}
alert(fn(2,3));
4、js的全局函数
(1)编码和解码
encodeURI() decodeURI()
encodeURIComponet() decodeURIComponent()
escape() unescape()
三者区别:
进行编码的符号范围不同吧,实际开发中常使用第一种
(2)强制转换
Number()
String()
Boolean()
(3)转成数字
parseInt()
parseFloat()
(4)eval()方法
将字符串当作脚本进行解析运行
//var str = "var a=2;var b=3;alert(a b)";
//eval(str); ##}
PRINT ("Custom Logic");
Three, JS's event
event
onclick: click event
onchange: event where domain content is changed
Requirement: realize secondary linkage
<select id="city"> <option value="bj">北京</option> <option value="tj">天津</option> <option value="sh">上海</option> </select> <select id="area"> <option>海淀</option> <option>朝阳</option> <option>东城</option> </select> <script type="text/javascript"> var select = document.getElementById("city"); select.onchange = function(){ var optionVal = select.value; switch(optionVal){ case 'bj': var area = document.getElementById("area"); area.innerHTML = "<option>海淀</option><option>朝阳</option><option>东城</option>"; break; case 'tj': var area = document.getElementById("area"); area.innerHTML = "<option>南开</option><option>西青</option><option>河西</option>"; break; case 'sh': var area = document.getElementById("area"); area.innerHTML = "<option>浦东</option><option>杨浦</option>"; break; default: alert("error"); } }; </script>
onfoucus:获得焦点的事件
onblur:失去焦点的事件
需求: 当输入框获得焦点的时候,提示输入的内容格式
当输入框失去焦点的时候,提示输入有误
onmouseover:鼠标悬浮的事件
onmouseout:鼠标离开的事件
需求:p元素 鼠标移入变为绿色 移出恢复原色
#d1{background-color: red;width:200px;height: 200px;}
onload:加载完毕的事件
等到页面加载完毕在执行onload事件所指向的函数
3. Default behavior of preventing events
IE:window.event.returnValue = false;
W3c: The passed event object.preventDefault();
//ie:window.event.returnValue = true; .preventDefault();
//IE tag
}else{
alert("ie");
window.event.returnValue = false;
}
// Returning false from the event can also prevent the default behavior of the event
4. Prevent the spread of events
IE: window.event.cancelBubble = true;
W3c: The passed event object.stopPropagation();
if(e&&e.stopPropagation){
# alert("ie");
4. JS bom
(1) window object
Pop-up method:
Prompt box: alert("prompt information");
Confirmation box: confirm("confirmation information" ; :prompt("Prompt information");
There is a return value: If you click Confirm to return the text of the input box, click Cancel to return null
var res = prompt("Please enter your password?");
alert(res );
open method:
window.open("url address");
open("../jsCore/demo10.html");
settimeout (function, millisecond value);
Settimeout (
Function () {
Alert ("xx");
},
3000
); The name of the device);
var timer;
var fn = function(){
alert("x"); );
};
var closer = function(){
clearTimeout(timer);
var closer = function(){
clearTimeout(timer);
setInterval(function, millisecond value) );
clearInterval(timer name)
var timer = setInterval(
function(){
alert("nihao");
},
20 00
);
var closer = function(){
clearInterval(timer);
clearInterval(timer);
">5 Jump to the home page in seconds. If it does not jump, pleaseClick here
type="text/javascript">
var time = 5;
var timer;
timer = setInterval(
function(){
var second = document.getElementById("second");
if(time>=1){
second.innerHTML = time;
time--;
}else{
clearInterval(timer);
location.href="../jsCore/demo10.html";
}
},
1000
);
(2)location
location.href="url地址";
(3)history
back();
forward();
go();
后一页
五、js的dom
1、理解一下文档对象模型
html文件加载到内存之后会形成一颗dom树,根据这些节点对象可以进行脚本代码的动态修改
在dom树当中 一切皆为节点对象
2、dom方法和属性
笔记见代码
相关推荐:
The above is the detailed content of A complete collection of unknown popular basic JavaScript knowledge (Collection). For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
