在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:wilfredinni/javascript-cheatsheet开源软件地址:https://github.com/wilfredinni/javascript-cheatsheet开源编程语言:Vue 80.9%开源软件介绍:CommentsExamples// This is an in-line comment.
/* This is a
multi-line comment */ Data typesBasicsJavaScript provides seven different data types:
VariablesExamples// declare a variable
var ourName;
// store values
myNumber = 5;
myString = "myVar";
// declare variables with the assignment operator
var myNum = 0;
// add, subtract, multiply and divide numbers
myVar = 5 + 10; // 15
myVar = 12 - 6; // 6
myVar = 13 * 13; // 169
myVar = 16 / 2; // 8
// increment and decrement numbers
i++; // the equivalent of i = i + 1
i--; // the equivalent of i = i - 1;
// decimals
var ourDecimal = 5.7; // float ES6 var, let and const
To ensure your data doesn't change, JavaScript provides a function Object.freeze to prevent data mutation. let obj = {
name: "FreeCodeCamp",
review: "Awesome",
};
Object.freeze(obj);
obj.review = "bad"; //will be ignored. Mutation not allowed
obj.newProp = "Test"; // will be ignored. Mutation not allowed
console.log(obj);
// { name: "FreeCodeCamp", review:"Awesome"} StringsExamples// escape literal quotes
var sampleStr = 'Alan said, "Peter is learning JavaScript".';
// this prints: Alan said, "Peter is learning JavaScript".
// concatenating strings
var ourStr = "I come first. " + "I come second.";
// concatenating strings with +=
var ourStr = "I come first. ";
ourStr += "I come second.";
// constructing strings with variables
var ourName = "freeCodeCamp";
var ourStr = "Hello, our name is " + ourName + ", how are you?";
// appending variables to strings
var anAdjective = "awesome!";
var ourStr = "freeCodeCamp is ";
ourStr += anAdjective; Escape sequences
The length of a string"Alan Peter".length; // 10 Split and Joinlet str = "a string";
let splittedStr = str.split("");
// [ 'a', ' ', 's', 't', 'r', 'i', 'n', 'g' ]
let joinedStr = splittedStr.join("");
// a string Index of a String//first element has an index of 0
var firstLetterOfFirstName = "";
var firstName = "Ada";
firstLetterOfFirstName = firstName[0]; // A
// find the las character of a string
var firstName = "Ada";
var lastLetterOfFirstName = firstName[firstName.length - 1]; // a ES6 Template Literalsconst person = {
name: "Zodiac Hasbro",
age: 56,
};
// Template literal with multi-line and string interpolation
const greeting = `Hello, my name is ${person.name}!
I am ${person.age} years old.`;
console.log(greeting);
// Hello, my name is Zodiac Hasbro!
// I am 56 years old. ArraysExamplevar sandwich = ["peanut butter", "jelly", "bread"][
// nested arrays
(["Bulls", 23], ["White Sox", 45])
]; Index of an arrayvar ourArray = [50, 60, 70];
var ourData = ourArray[0]; // equals 50
// modify an array with indexes
var ourArray = [50, 40, 30];
ourArray[0] = 15; // equals [15,40,30]
// access multi-dimensional arrays with indexes
var arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[[10, 11, 12], 13, 14],
];
arr[3]; // [[10,11,12], 13, 14]
arr[3][0]; // [10,11,12]
arr[3][0][1]; // 11 Manipulate arrays with reverse, push, pop, shift and unshift// reverse an array
[1, "two", 3].reverse(); // [ 3, 'two', 1 ]
// push() to append data to the end of an array
var arr = [1, 2, 3];
arr.push(4); // arr is now [1,2,3,4]
// pop() to "pop" a value off of the end of an array
var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();
console.log(oneDown); // Returns 6
console.log(threeArr); // Returns [1, 4]
// shift() removes the first element of an array
var ourArray = [1, 2, [3]];
var removedFromOurArray = ourArray.shift();
// removedFromOurArray now equals 1 and ourArray now equals [2, [3]].
// unshift() adds the element at the beginning of the array
var ourArray = ["Stimpson", "J", "cat"];
ourArray.shift(); // ourArray now equals ["J", "cat"]
ourArray.unshift("Happy"); // ourArray now equals ["Happy", "J", "cat"] Remove any element with splice// first parameter is the index, the second indicates the number of elements to delete.
let array = ["today", "was", "not", "so", "great"];
array.splice(2, 2);
// remove 2 elements beginning with the 3rd element
// array now equals ['today', 'was', 'great']
// also returns a new array containing the value of the removed elements
let array = ["I", "am", "feeling", "really", "happy"];
let newArray = array.splice(3, 2);
// newArray equals ['really', 'happy']
// the third parameter, represents one or more elements, let us add them
function colorChange(arr, index, newColor) {
arr.splice(index, 1, newColor);
return arr;
}
let colorScheme = ["#878787", "#a08794", "#bb7e8c", "#c9b6be", "#d1becf"];
colorScheme = colorChange(colorScheme, 2, "#332327");
// we have removed '#bb7e8c' and added '#332327' in its place
// colorScheme now equals ['#878787', '#a08794', '#332327', '#c9b6be', '#d1becf'] Copy an array with slice// Copies a given number of elements to a new array and leaves the original array untouched
let weatherConditions = ["rain", "snow", "sleet", "hail", "clear"];
let todaysWeather = weatherConditions.slice(1, 3);
// todaysWeather equals ['snow', 'sleet'];
// weatherConditions still equals ['rain', 'snow', 'sleet', 'hail', 'clear'] indexOflet fruits = ["apples", "pears", "oranges", "peaches", "pears"];
fruits.indexOf("dates"); // -1
fruits.indexOf("oranges"); // 2
fruits.indexOf("pears"); // 1, the first index at which the element exists Accessing Nested Arraysvar ourPets = [
{
animalType: "cat",
names: ["Meowzer", "Fluffy", "Kit-Cat"],
},
{
animalType: "dog",
names: ["Spot", "Bowser", "Frankie"],
},
];
ourPets[0].names[1]; // "Fluffy"
ourPets[1].names[0]; // "Spot" ES6 Includes to Determine if an Array Contains an Elementlet fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango"); // true ES6 The Spread Operator// The ES5 code below uses apply() to compute the maximum value in an array.
var arr = [6, 89, 3, 45];
var maximus = Math.max.apply(null, arr); // 89
// ...arr returns an unpacked array. In other words, it spreads the array.
const arr = [6, 89, 3, 45];
const maximus = Math.max(...arr); // 89
// [...new Set(arr)] = unique value array
const arr = [1, 2, 2, 3, 3, 4, 5, 5];
const uniq = [...new Set(arr)]; // [1,2,3,4,5]
// copy an array
let thisArray = [true, true, undefined, false, null];
let thatArray = [...thisArray];
// thatArray equals [true, true, undefined, false, null]
// thisArray remains unchanged, and is identical to thatArray
// combine arrays
let thisArray = ["sage", "rosemary", "parsley", "thyme"];
let thatArray = ["basil", "cilantro", ...thisArray, "coriander"];
// thatArray now equals ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander'] ES6 Destructuring Arrays to Assign Variablesconst [a, b] = [1, 2, 3, 4, 5, 6];
console.log(a, b); // 1, 2
// it can access any value by using commas to reach the desired index
const [a, b, , , c] = [1, 2, 3, 4, 5, 6];
console.log(a, b, c); // 1, 2, 5
// to collect the rest of the elements into a separate array.
const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
console.log(a, b); // 1, 2
console.log(arr); // [3, 4, 5, 7] Array MethodsMap全部评论
专题导读
上一篇:mysqljs/mysql: A pure node.js JavaScript Client implementing the MySQL protocol.发布时间:2022-06-24下一篇:tapmodo/Jcrop: Jcrop - The Javascript Image Cropping Engine发布时间:2022-06-24热门推荐
热门话题
阅读排行榜
|
请发表评论