jQuery Fundamentals 笔记 01

jQuery 是目前使用最广泛的 javascript 函数库,全球前10000个访问最高的网站中,有65%使用了 jQuery,是目前最受欢迎的 JavaScript 库。之所以这么受欢迎,是因为 jQuery 能让你做到 “Write less, do more”.

在线教程中,名气较大有 jQuery Fundamentals , 我准备写些相关笔记方便自己复习。

JavaScript Basics

how to write comments

// I love emmet!
var foo;

// 用于单行

/* hehe
   hehe
*/

function sass(foo /* blabal */, bar /* blabla */) {
}

/* */ 用于多行或者行内

The building blocks of JavaScript

Var

提问: 变量名不能以什么开头?

Functions

var addTwoNum = function() {
    return a+b;
}

function expression

function addTwoNum(a, b) {
    return a+b;
}

function declaration

两者都可以表达函数,但是最好用前者,为什么?

Functions and variable scope

简单说就是警惕 global, 变量前面没事就写个 var,安全第一~

Objects

以下五种不是 Objects

  • strings(text)
  • booleans(true/false)
  • numbers
  • undefined
  • null

Accessing properties

var person = {
    name: 'bush',
    age: 3
};

console.log('person[age]');
console.log(person.name);

Bracket notation and dot notation

Objects methods

var person = {
    name: 'foo',
    age: 3,
    greet: function(name) {
        console('Hi' + name);
        }
    };

person.greet(person.name);

The meaning of this

window.color = "red";
var o = {color: "blue"};

function sayColor() {
    alert(this.color);
}

sayColor();     //"red"

o.sayColor = sayColor;
o.sayColor();   //"blue"

用下面的两个函数可以强制改变 this 指定的对象: .call() vs .apply() 这两个函数实在是太像了,以至于看完本章以后脑子里面还是一片糨糊,也可以看看 stackoverflow

1
2
foo.apply(person, ["hello", "wow"]);
foo.call(person, "hello", "wow");

不管是哪一个,都有能对某个对象直接执行本身不具备的 method 而不必修改对象本身的妙用。

Objects in jQuery

简单说就是利用了 CSS 的选择符的便利性

Arrays

注意索引从0开始即可

Logic and Truthiness

1 is truthy, and there are five values in JavaScript that is falsy.

  • null
  • NaN
  • 0
  • undefined

The Ternary Operator

1
var name = (dim === "true") ? "yes" : "no";

结语

好吧,成功地把以前的语文老师改造成体育老师了,顺便也打了几行代码,感觉神清气爽多了~ 打键盘,更健康!

Comments