markdown JavaScript基础知识2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown JavaScript基础知识2相关的知识,希望对你有一定的参考价值。
### String Functions
```javascript
//Casing
"shout".toUpperCase(); //=> "SHOUT"
"SHOUT".toLowerCase(); //=> "shout"
//Replace
"i love to code!".replace("code", "eat") //=> "i love to eat!"
//Will only replace the first occurence of the word or phrase to
//subsitute!
//Convert string to number
Number('78') //=> 78
parseInt('78') //=> 78
//Slice
//returns a new string that is cut from a starting point argument to the
//ending point argument
"hello, my name is parm".slice(18, 22) //=> "parm"
//you can also use negative indices to start at the END of a string
//Split
//converts an string into an array by the argument provided
"hello, apple, banana".split(',') //=> ['hello', 'apple', 'banana']
```
---
### Functions Review
* BASIC SYNTAX: (function declaration)
```javascript
function nameOfFunction() {
//code
return something;
}
```
* lowerCamelCase is used for the naming convention for JS functions (no snake_cased!!)
* A function in JS will always return `undefined`, unless a return value is given.
#### Arrow functions
```javascript
[1,2,3,4].filter( i => i % 2 === 0) // [2,4], no need for explicit return statement
[1,2,3,4].filter( i => {return i % 2 === 0} ) // [2,4], if need to specify a return value
//can be written as:
[1,2,3,4].filter( i => {
return i % 2 === 0
})
```
* To call a function and execute it: `functionName();`
* Needs the `();`
* calling the function without the `();`, will just return the entire function declaration
* Functions can be written as above or as a **fucntion expression**:
```javascript
var functionName = function() {
return something;
}
```
* ** Function declarations are hoisted to the top of the scope**, function expressions are not!
* This means you can call a function before declaring it, if it was written as a function
declaration, if written as a function expression, the call to the function will error out!
* You can give a function default params: `function parm(name="Parm") {}`
---
### Control Fow
* if/else statement
```javascript
if (condition) {
} else if (condition2) {
} else {
}
```
* switch statements
```javascript
switch (expression) {
case n:
//code
break;
case m:
//code
break;
default:
//code
}
```
---
### Arrays
* use `.push(element)` to add an element to an array
* Removing an element in a JS array
```javascript
var fishArray = ['angel', 'clown', 'drum', 'surgeon'];
fishArray.splice(indexToStart, numberOfElementsToRemove, elementToAdd (optional));
fishArray.splice(1, 2, 'mandarin') //removes 'clown' and 'drum' and adds 'mandarin' to the end
```
* Iteration using `forEach`
```javascript
var array = [ ... ];
array.forEach(function(element, index) {
//forEach will yield each element in the array to your function
//a second argument to this function is the index of the element
})
```
#### Looping
* loops include `for`, `while`, `do-while`
```javascript
//For loop
for([initialization]; [condition]; [iteration]) {
//loop body
}
for(var i = 1; i < 100; i++) {
//stuffs
}
//While loop: continue loop execution until condition is false
while([condition]) {
//stuffs
}
//Do-while ensures that a loop is executed atleast once
do {
} while ([condition])
```
---
### Hashes
* Initialize the object:
```javascript
var obj = {};
//or
var obj = new Object();
obj["name"] = "Parm";
obj["age"] = 25;
obj["occupation"] = "Developer";
// To access a value, use the key:
obj["name"] //=> "Parm"
//Deleting a value
delete obj["age"]
//You can the value by just over-writing the value stored in the key
obj["age"] = 100;
//To check it this object is empty, there is no .empty? method, just check the keys:
Object.keys(obj) //returns an array for all the keys in the object
//Iterating over an object
for (variableName in objectName) {
//code
}
```
以上是关于markdown JavaScript基础知识2的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript全面知识总结:JS正则表达式+markdown语法+前端大厂面试题+项目实例
markdown JavaScript - Lezione 2
markdown JavaScript和DOM,第2部分:事件
markdown (原始)Angular 2包括外部JavaScript库
swift 笔记:iOS与JavaScript的交互(二):JavaScriptCore:2。 markdown转成html中加载Markdown.Converter.js