求每个班级的男女比率
//1班学生
var students1 = [{
name:'魏文庆',
gender:1
},{
name:'蔡剑飞',
gender:1
},{
name:'张贤',
gender:0
}
//...
];
//2班同学
var students2 = [{
name:'陆忠芳',
gender:0
},{
name:'严跃杰',
gender:1
},{
name:'胡雪亮',
gender:1
}
//...
];
//求1班的男女比率
var maleCount1 = 0,
femaleCount1 = 0,
length1 = students1.length,
students1,
ratiol;//比率
//遍历1班的学生
for(var i = 0l i < length1; i++){
student1 = students1[i];
if (student1.gender === 1){
//如果是男生,男生数量加1
maleCount1++;
}else{
//否则女生数量加1
femaleCount1++;
}
}
ratio1 = maleCount1/femaleCount1;
//求2班的男女比率
var maleCount2 = 0,
femaleCount2 = 0,
length2 = students2.length,
students2,
ratiol;//比率
//遍历2班的学生
for(var i = 0l i < length2; i++){
student2 = students2[i];
if (student1.gender === 1){
//如果是男生,男生数量加1
maleCount2++;
}else{
//否则女生数量加1
femaleCount2++;
}
}
ratio2 = maleCount2/femaleCount2;
function ratio(students){
var maleCount = 0,
femaleCount = 0,
length = students.length,
student,
ratio;
for (var i = 0;i < length; i++){
student = students[i];
if(student.gender === 1){
maleCount++;
}else{
femaleCount++;
}
}
ratio = maleCount / femaleCount;
return ratio;
}
var ratio1 = ratio(students1);
var ratio2 = ratio(students2);
var ratio3 = ratio(students3);
...
函数语法
function add(number0,number1){
var sum = number0 + number1;
return sum;
}
var x = add(2,3);
function 函数名(形参列表){
执行代码
}
函数名([实参列表])
函数定义
//1.函数声明
function add(number0,number1){
var sum = number0 + number1;
return sum;
}
//2.函数表达式
var add = function(number0,number1){
var sum = number0 + number1;
return sum;
}
var x = add(4,6);//10
函数调用
function add(number0,number1){//number0=2,number1=3把实参列表复制给了形参列表
var sum = number0 + number1;//sum = 5
return sum;
}
var x = add(2,3);//x=5
函数参数
//实参数量少于形参时
function add(number0,number1){ //number0 = 2,number1 = undefined
var sum = number0 + number1; //
return sum;
}
var x = add(2);//NaN
//实参数量多于形参
function add(number0,number1){ //number0 = 2,number1 = 3
var sum = number0 + number1; //
return sum;
}
var x = add(2,3,4);//5
调用函数时,有个隐藏的变量
//参数不定的代码求和
add(2,3);//5
add(2,3,4);//9
add(2,3,4,5);//14
function add(){
var length = arguments.length,
sum = 0,
parameter;//参数
for(var i = 0;i < length;i++){
parameter = arguments[i];
sum = sum + parameter;
}
return sum;
}
//参数为原始类型:值传递
function increment(number){
number = number +1;
return number;
}
var a = 1;
var x = increment(a);//2
a;//1
//参数为对象类型:引用传递,在函数中修改对象会影响到原对象
function increment(person){
person.age = person.age + 1;
return person;
}
var jerry = {name:'jerry',age:1};
var x = increment(jerry);//{name:'jerry',age:2}
jerry;//{name:'jerry',age:2}
作用域
var zhoujielun = {
name:"周杰伦",
gender:1
};
//函数创建了一个作用域,变量的作用域其实就是限制了变量起作用的范围
function class1(){
var zhoujielun = {
name:"周杰伦",
gender:0
};
zhoujielun.name = "周杰";
zhoujielun.gender = 1;
}
class1();
zhoujielun;//{name: "周杰伦", gender: 1}
var zhoujielun = {
name:"周杰伦",
gender:1
};
function class1(){
zhoujielun.name = "周杰";
zhoujielun.gender = 1;
}
class1();
zhoujielun;//{name: "周杰", gender: 1}
作为对象属性
var point = {
x:1;
y:1;
move:function(stepX,stepY){
//point.x += stepX;
//point.y += stepY;
this.x += stepX;
this.y += stepY;
//this表示调用这个函数的对象
}
};
point.move(2,1);//(3,2)
构造函数
function Point(x,y){ //创建一个空对象{}
this.x = x; //{x:1}
this.y = y; //{x:1,y:1}
this.move = function(stepX,stepY){ //{x:1,y:1,move:function(stepX,stepY){}}
this.x += stepX;
this.y += stepY;
}
}
//构造函数的返回结果相当于return了this
var point = new Point(1,1);//{x:1,y:1,move:function(stepX,stepY){}}
var point2 = new Point(2,2);//{x:2,y:2,move:function(stepX,stepY){}}
var point3 = new Point(3,3);//{x:3,y:3,move:function(stepX,stepY){}}
//只需要创建一个move方法公用
原型
function Point(x,y){
this.x = x;
this.y = y;
}
Point.prototype.move = function(stepX,stepY){
this.x += stepX;
this.y += stepY;
};
var point = new Point(1,1); //{x:1,y:1}
point.move(2,1);//{x:3,y:2} 构造出来的这个对象肯定是有个隐藏的属性,指向它公共的地方,指向它构造函数的原型属性