FCC-Basic JavaScript

时隔一个多月开始继续学习前端的内容,一个月之间主要精力花在家庭监控这个项目上,这个链接里面有所有关于项目的内容,很适合有一点点linux知识的同学们组队学习,另外还有在教同学英语。前端的具体操作多少有点生疏了,概念还是存在的,现在开始学JavaScript就意味着之后的网页会有更加多样性的变化,毕竟HTML,CSS更多的作用只是UI,jQuery又是在JavaScript的基础上做的。所以可以认为JavaScript是前端学习的第一个大点。这篇博客里面主要记录FCC的Basic JavaScript部分的学习笔记。
1. JavaScript的注释和C语言的一样,//或者/**/
2. JavaScript provides seven different data types which are undefined, null, boolean, string, symbol, number, and object. JavaScript closes a statement with semicolons. Declare a variable using ‘var’. 12 is a number, “12” is a string.
3. Assignment goes from right side of the equal sign to the left side.
4. It is common to initialize a variable to an initial value in the same line as it is declared, like this: var myVar = 0;
5. When JavaScript variables are declared, they have an initial value of undefined. Math operation to a undefined variable will output “NaN”, meaning “not a number”.
6. Variables and function names are case sensitive. Just give the name following the law of camel case, var thisIsACamelCaseName = "Hello World!"
7. Some numeric operations: +, -, , /, ++, –, %(reminder operation), +=, -=, =, /=.
8. Not all real numbers can accurately be represented in floating point.
9. % does not work properly with negative numbers, so that it’s not modulus.
10. Escape a quote from considering it as an end of string quote by placing a backslash () in front of the quote, like: var sampleStr = "Alan said, \"Peter is learning JavaScript\".";.
11. String values in JavaScript may be written with single or double quotes, so long as you start and end with the same type of quote, and single and double quotes are functionally identical in JavaScript. 如果用单引号去括一个string,那么字符串里面出现的双引号就不需要用(\)转换字符。
12. escape table

CodeOutput
\’single quote
\”double quote
\\backslash
\nnewline
\fform feed
\bbackspace
\ttab
\rcarriage return

13. When the + operator is used with a String value, it is called the concatenation operator. It is also applied to +=. Both + and += can be used in one concatenation statement.
14. You can find the length of a String value by writing .length after the string variable or string literal.
15. Bracket notation is a way to get a character at a specific index within a string. The index used inside bracket starts from zero.
16. In JavaScript, String values are immutable, which means that they cannot be altered once created. Note that this does not mean that myStr cannot be changed, just that the individual characters of a string literal cannot be changed. The only way to change myStr would be to assign it with a new string. And adding some more chars in the end of a existing string is another story.
17. You start an array declaration with an opening square bracket, end it with a closing square bracket, and put a comma between each entry, like this:
var sandwich = ["peanut butter", "jelly", "bread"]
.
18. The entries of arrays are mutable and can be changed freely, while the String is immutable.
今天就学到这里吧,主要学了comment, numeric operations, string, array。内容不多。同时推荐一本书,JavaScript高级程序设计。作者的视角很好,对语法的解释很系统,所以看得时候需要耐心,是本好书。感觉比《Thinking in Java》的写作风格要更加活泼,让人接收起来更快,从零开始学的好教材。


  1. An easy way to append data to the end of an array is via the push() function. var arr = [1,2,3];arr.push(4);
  2. .pop() is used to “pop” a value off of the end of an array. We can store this “popped off” value by assigning it to a variable.
  3. .shift() is used to remove the first element from an array. unshift() is used to add an element to the beginning of an array.
  4. In JavaScript, we can divide up our code into reusable parts called functions. That is one of the basic reasons that we need to have founctions.
  5. Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or “passed”) into a function when it is called are known as arguments. Note that the difference between arguments and parameters.
  6. In JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code. Variables which are used without the var keyword are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with var.
  7. Variables which are declared within a function, as well as the function parameters have local scope. That means, they are only visible within that function. Here is a local variable example:

    function myTest() {
      var loc = "foo";
      console.log(loc);
    }
    myTest(); // "foo"
    console.log(loc); // "undefined"
  8. It is possible to have both local and global variables with the same name. When you do this, the local variable takes precedence over the global variable.
  9. We can take the return value of a function and assign it to a variable.
  10. Booleans may only be one of two values: true or false. They are basically little on-off switches, where true is “on” and false is “off.” These two states are mutually exclusive.
  11. The most basic operator is the equality operator ==. The equality operator compares two values and returns true if they’re equivalent or false if they are not.

    1   ==  1    // true
    1   ==  2    // false
    1   == '1'   // true
    "3"  ==  3    // true
  12. Strict equality (===) is the counterpart to the equality operator (==). Unlike the equality operator, strict equality tests both the data type and value of the compared elements.

    1   ===  1    // true
    1   ===  2    // false
    1   === '1'   // false
    "3"  ===  3    // false
  13. Like the equality operator(!=), the inequality operator will convert data types of values while comparing. This shows us the data type design in JavaScript is very loose.

    1 != 2      // true
    1 != "1"    // false
    1 != '1'    // false
    1 != true   // false
    0 != false  // false
  14. It means “Strictly Not Equal” and returns false where strict equality would return true and vice versa. Strict inequality will not convert data types.
  15. Like the equality operator, >, >=, <, <= will convert data types of values while comparing.
  16. The logical and operator (&&) returns true if and only if the operands to the left and right of it are true. The logical or operator (||) returns true if either of the operands is true. Otherwise, it returns false.
  17. If you have many options to choose from, use a switch statement. A switch statement tests a value and can have many case statements which defines various possible values. Statements are executed from the first matched case value until a break is encountered. Case values are tested with strict equality (===)

    switch (num) {
      case value1:
        statement1;
        break;
      case value2:
        statement2;
        break;
    ...
      case valueN:
        statementN;
        break;
    }

    今天干了一天和英语课有关的事情,看了一季的lip sync battle,搜索合适的歌。没有抽太多时间写FCC,明天早上继续辣!现在11:30开始就断网了。加油!


新的一天加油!
1. array的length属性是可写的!所以如果更改length,会直接影响到array的数据。
2. There are two ways to access the properties of an object: the dot operator (.) and bracket notation ([]), similar to an array. If the property of the object you are trying to access has a space in it, you will need to use bracket notation. Another use of bracket notation on objects is to use a variable to access a property. This can be very useful for iterating through lists of the object properties or for doing the lookup. Note that we do not use quotes around the variable name when using it to access the property because we are using the value of the variable, not the name
3. After you’ve created a JavaScript object, you can update its properties at any time just like you would update any other variable. You can use either dot or bracket notation to update.
4. You can add new properties to existing JavaScript objects the same way you would modify them. Here’s how we would add a “bark” property to ourDog: ourDog.bark = "bow-wow";
5. We can also delete properties from objects like this: delete ourDog.bark;
6. Using Objects for Lookups:

```
// Setup
function phoneticLookup(val) {
  var result = "";
  // Only change code below this line
  var lookup = {
    "alpha" : "Adams",
    "bravo" : "Boston",
    "charlie" : "Chicago",
    "delta" : "Denver",
    "echo" : "Easy",
    "foxtrot" : "Frank"
  };
  result = lookup[val];
  // Only change code above this line
  return result;
}
// Change this value to test
phoneticLookup("charlie");
```

7. We can use the .hasOwnProperty(propname) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.
8. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
9. The sub-properties of objects can be accessed by chaining together the dot or bracket notation.
10. JavaScript has a Math.random() function that generates a random decimal number between 0 (inclusive) and not quite up to 1 (exclusive). Thus Math.random() can return a 0 but never quite return a 1.
11. A founction to generate a random whole number inside given range [myMin, myMax).

```
function randomRange(myMin, myMax) {
  return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
}
```

12. Regular expressions: /the/gi => case insensitive, find the pattern globally, the pattern itself is “the”. \d === [0-9], \s <=> white space(The whitespace characters are ” ” (space), \r (the carriage return), \n (newline), \t (tab), and \f (the form feed).). Invert any match by using the uppercase version of the regular expression selector, like, \S <=> not white space, \D <=> not a number.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值