The sixth web front-end training notes [P24-p26]

JavaScript

Basic grammar

1. Built in objects

var str="Hello World";
console.log(str);

substring(m,n) returns a given string starting at position m and ending at position n. If the parameter is omitted, it means to get to the end of the string

console.log(str.substring(3));
console.log(str.substring(3,5));

toLowerCase() converts all characters in the string to lowercase

console.log(str.toLowerCase());

toUpperCase() converts all characters in the string to uppercase

console.log(str.toUpperCase());

Math. Random number

console.log(Math.random());

Math.ceil() rounded up, greater than the maximum integer

console.log(Math.ceil(1,2));

Math.floor() rounded to the smallest, less than the smallest integer String

console.log(Math.floor(1,2));

Get Date object

var date=new Date();
console.log(date);

getFullYear() year

console.log(date.getFullYear());

getMonth() month

console.log(date.getMonth()+1);

getDate() day

console.log(date.getDate());

getHours (

 console.log(date.getHours());

getMinutes

 console.log(date.getMinutes());

getSeconds() seconds

console.log(date.getSeconds());

Time localization

console.log(date.toLocaleDateString());

2. Object

2.1 creation of objects

(1) Creating objects in literal form

var obj1={};
console.log(obj1);
var obj2={
	name:"zhangsan";
	age:18;
};
console.log(obj2);

(2) Created by new object()

var obj3=new Object();
console.log(obj3);

(3) Created through the Create method of the Object object

var obj4=Object.create(null);
console.log(obj4);

2.2 operation of objects

Get the property of the object (get null if the property does not exist)

Object name Attribute name

console.log(obj2.name);

Set the property of the object (if the property exists, modify the property value. If the property does not exist, add a new property value)

Object name Attribute name = value;

obj2.age=20;
consolle.log(obj2);
obj2.upwd="123456";
console.log(obj2);

2.3 serialization and deserialization of objects

Serialization: converts JS objects (JOSN objects) into JSON strings

var variable name = JSON Stringify (object);

//JSON object
var obj={
		name:"zhangsan";
		pwd:"123456";
		age:18
};
obj.name="lisi";
console.log(obj);
//Convert JSON objects to JSON strings
var objToStr=JSON.stringify(obj);
console.log(objToStr);
console.log("==========");
//JSON string
var jsonStr='{name:"zhangsan","pwd":"123456","age":18}';
jsonStr="lisi";
console.log(jsonStr);

Deserialization: convert JSON string to JS object (JSON object)

var object name = JSON Parse (JSON string);

var strToObj=JSON.parse(jsonStr);
console.log(strToObj);

2.4 this

Who calls the function and whom does this refer to

(1) Directly call the function, and this represents the global width object

function test() {
	console.log("This is a test method...");
	console.log(this);
}
test();

(2) Call the function in the object. this represents the object itself

var o={
	name:"zhangsan";
	age:18;
	sayHello:function() {
		console.log("How do you do~");
		console.log(this);
	}
}
o.sayHello();

JS event

1. Several nouns in the event

Event source: what element / tag is bound to the event

Event name: what event is bound to

Event listening: browser window

Execute function: what code needs to be executed after the event is triggered

2. Common event types

onload: triggered immediately after the page or image is loaded

onblur: element loses focus

onfocus: the element gets the focus

onclick: click an object with the mouse

onchange: the user changes the content of the domain

onmouseover: move the mouse over an element

onmouseout: the mouse moves away from an element

onkeyup: the key of a keyboard is released

onkeydown: the key of a keyboard is pressed

Keywords: Front-end

Added by DaPrince on Fri, 11 Feb 2022 18:16:04 +0200