JavaScript学习笔记

JavaScript学习笔记

虽然以前用过不同的JS库,但从未系统得学习过JS语言。今天幸好完成了Codecademy上的Learn JavaScript的基本课程,记录一下基本用法。

(图片来源:PlanetSourceCode)

执行语句结尾加”;”

定义并可以改变变量

var morningAlarm = '6:30AM';
morningAlarm = "7:20AM";

if 条件

if () { } else { }

可以用switch代替if

var moonPhase = 'full';

switch(moonPhase) {
  case 'full':
    console.log('Howwlll');
    break;
  case 'mostly full':
    console.log('Arms and legs are getting hairier');
    break;
  case 'mostly new':
    console.log('Back on two feet');
    break;
  default:
    console.log('Invalid moon phase');
    break;
}

是否等同用"===” 或 “!==”

自定义功能(可调用其他功能)

var orderCount = 0;
function takeOrder(topping, crustType) {
  orderCount = orderCount +1;
  console.log('Order: ' + crustType + 'pizza' + topping);
}

function getSubTotal(itemCount) {
  return itemCount * 7.5;
}

function getTax() {
  return getSubTotal(orderCount) * 0.06;
}

function getTotal() {
  return getSubTotal(orderCount) + getTax();
}
takeOrder('big','crusty');
takeOrder('superBig','soft');
takeOrder('small','hard');

console.log(getSubTotal(orderCount))
console.log(getTotal())

功能functions里的变量不能全局调用

阵列操作array: console.log(list[0]); list.length(); list.pop(); list.push(‘d’,‘e’)

var list = ['a','b','c'];

循环for 和 while

var myPlaces = ['Place2','London','Berlin'];
var friendPlaces = ['Place1','Place2','Spot3'];

for (var i = 0; i<myPlaces.length; i++) {
  console.log(myPlaces[i]);
  for (var j =0; j<friendPlaces.length; j++) {
    if (myPlaces[i] === friendPlaces[j])  {
      console.log(myPlaces[i]);
    }
  }
}

变量操作

myPlaces.push("Beijing") // add to end
myPlaces.pop() // delete last
myPlaces.unshift("Frankfurt") // add to start
myPlaces.shift() // delete first
myPlaces.length // variable length

调用html元素

var example-class = document.getElementsByClassName('example-class-name');
var header = document.getElementsByTagName('header');
header.innerHTML = "<p> This is header </p>"; // change element content

jQuery简化

$('example-tag-name'); // tag name
$('.example-class-name'); // class name
$('#example-id-name'); // id name
$('tag-name.class-name'); // combination of tag and class name

Get element and change its attribute

var firstPlan = document.getElementById('plan1');
firstPlan.className = "delete"; // change class="" to "delete"
firstPlan.textContent = "Finished plan";

Siblings and parents

var firstPlan = document.getElementById('plan1');
var secondPlan = firstPlan.nextSibling; // next neighbour node
secondPlan.ClassName = "finish";
var parentPlan = firstPlan.parentNode; // upper level node
var childPlan1 = parentPlan.firstChild; // child node

jQuery 显示隐藏示例

function main() {
  $('.projects').hide(); // $('.projects').fadeIn(400);
  $('.projects-button').on('click', function() {
    $(this).toggleClass('active'); // toggle on and off
    $(this).text('Projects Viewed'); 
    $(this).next().slideToggle(400); // next = following class
  });
}

$(document).ready(main);

系统通知

alert("It's done")
confirm("You are going to close the window")
prompt("What's your name?")

创建object

var car = new Object();
    car.color = "red";
    car.changeColor = function(anothercolor) {
	this.color = anothercolor;
}

OR to use literal notation

var car = {
    color: "red", // separate attribut and valus with ':', and variables with ','
    maker: "Ford",
    changeColor: function(anothercolor) {
	this.color = anothercolor;
	document.write("The car is now" + this.color);
    	}
}

Events

  • Mouse events: onclick, onbdlclick, onmouseover, onmouseout, ondrag
  • Keyboard events: onkeydown, onkeyup
  • Browser/object events: onload, onunload, onfocus, onblur, onerror, onresize, onscroll

example click event

document.getElementsByClassName("plans").onclick = function linkClick () {
								this.className = "finish";
}
Z. Lu avatar
Z. Lu
Data scientist, bioinformatician, retro fan and web lover.
comments powered by Disqus