if
执行到最后一个才会 return,且return的值是最后判断的。
else if
带顺序的判断,判断为真后,后续的else if 是不会执行的,且return的值是当前返回值。
logical add
&&
logical or
||
else 在循环内会循环返回
//初始化变量
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUp(firstName, prop){
// 请把你的代码写在这条注释以下
for(var i=0; i< contacts.length; i++){
if(contacts[i].firstName == firstName)
{
if(contacts[i].hasOwnProperty(prop))
{
return contacts[i][prop];
}
else return"No such property";
}
}
return "No such contact";
// 请把你的代码写在这条注释以上
}
// 你可以修改这一行来测试你的代码
lookUp("Sherlock", "likes");
匹配文字、数字、符号
times = orgin.match(experssion).length
orgin:原始语句
experssion :要查找的
# /****/gi
g:global 从头开始全部;
i: ignore 忽略大小写;
# /\d+/gi
\d: 匹配数字 数字选择器
+: 允许匹配一个或多个数字
# /\s+/g
空白字符有 " " (空格符)、\r (回车符)、\n (换行符)、\t (制表符) 和 \f (换页符)
\s 匹配任何空白字符,\S 匹配任何非空白字符。(不带+号)
构造函数
通常使用大写字母开头,以便把自己和其他普通函数区别开。
var Car = function() {
this.wheels = 4;
this.engines = 1;
this.seats = 1;
};
在 构造函数 中, this 指向被此 构造函数 创建出来的 对象 。所以,当我们在 构造函数 中写:this.wheels = 4;
它创建出来的新对象将带有 wheels 属性,并且赋值为 4.
构造函数 中添加参数
var Car = function(wheels, seats, engines) {
this.wheels = wheels;
this.seats = seats;
this.engines = engines;
};
现在,我们可以在调用 构造函数 时传入一组 参数 了。
var myCar = new Car(6, 3, 1);
创建出下面的对象:
{
wheels: 6,
seats: 3,
engines: 1
}