首页 > 代码库 > JS基础

JS基础

// statement (语句)
// variable (变量)
// assignment(赋值)
// declare(申明)
// typing(类型申明)

var a, b;

var a;
var b;

var a = 1;
var b = 2;

var a, b;
a = 1;
b = 2;

// array(数组)
// element(元素)

var beatles = array();//括号内为元素个数
// array[index] = element;
beatles[0] = "john";

var beatles = array("joho","ok");
var beatles = ["john","paul"];

var lenoon = []; //数组赋值给另一个数组的元素
lenoon[0] = beatles;

lenoon[0][1];

// Object(对象)
var beatles = Object();
beatles.name = "john"
beatles.year = "1994"

// 对象代替数组
var beatles = Object();
var beatles = { name:"john",year:"1992"};

var beatles = {}; // 数组声明为对象
beatles.vocalist = lenoon;

beatles.vocalist.name = "john";

// 算数操作符
var a = "ff";
var b = "hah" + a;

var year = 2010;
var message = "this year is";
message += + year;
alert(message);

// 条件语句
if (condition) {
statements;
};

if (1>2) {
alert("hello");
}else{
alert("fuck");
}

var a, b;
a = "a";
b = "alert";

if (a == b) {
alert("same");
}else{
alert("false");
}

// 逻辑操作符
var a = 4;
if (a >= 5 && a <= 10) {
alert("a");
}

// 循环语句
var count = 1;
while (count < 11){
alert(count);
count++;
}

var count = 1;
do {
alert(count);
count++;
}while(count < 1);

for (var count = 1; count < 3; count++ ) {
alert(count);
}

// 数组循环
var beatles = Array("a","b","c","d");
for (var count = 0; count < beatles.length; count++) {
alert(beatles[count]);
};

// 函数:多次调用的代码可以封装成为函数
function shout () {
var beatles = Array("a","b","c","d");
for (var count = 0; count < beatles.length; count++) {
alert(beatles[count]);
};
}

shout()// 调用

//函数给变量赋值
function c(temp) {
var resule = temp - 32;
resule = resule / 1.8;
return = resule;
}

var temp_1 = 95;
var temp_2 = c (temp_1);
alert(temp_2);

JS基础