1字符串拼接
var a = 100+10 //110
var b = 100+'10' //10010
2 == 运算符
100 == ‘100’ //true
0 == '' //true
3 if 语句
var a = true
if (a) {
// ....
}
var b = 100
if (b) {
//....
}
var c = ' '
if (c) {
//...
}
4 逻辑运算符
console,log(10 && 0) //0
console.log( ''|| 'abc') //'abc'
console.log(!window.abc) //true
//判断一个变量会当做true还是false
var a = 100
console.log (!!a)
function Foo (name,age ) {
this.name = name
this.age =age
this.class = 'class-1'
//return this 默认有这一行
}
//创建对象
var f = new Foo ('zhangsan',20)
构造函数扩展
var a = [] 是 var a = new Array()的语法糖
var b = {} 是 var b = new Object()的语法糖
function Foo(){ .. }其实var foo = new function (...)
使用instanceof判断一个函数是否是某个变量的构造函数
//构造函数
function Foo(name,age) {
this.name = name
}
Foo.prototype.alertName = function () {
alert (this.name)
}
//创建示例
var f = new Foo ("张三”,90)
f.printName = function () {
console.log(this.name)
}
//测试
f.printName()
f.alertName()
循环对象自身的属性
var item
for (item in f) {
//高级浏览器已经在for in 中屏蔽了来之原型的属性
//但是这里建议大家还是加上这个判断,保证程序的健壮性
if (f.hasOwnProperty(item)) {
console.log(item)
}
}
function Elem(id) {
this.elem = document.getElementById(id)
}
Elem.prototype.html = function (val) {
var elem = this.elem
if (val) {
elem.innerHtml = val
return this
}else {
return elem.innerHTML
}
}
Elem.prototype.on = function (type,fn) {
var elem = this.elem
elem.addEventListener(type,fn)
}
var div1 = new Elem('div1')
作用域和闭包
fn ()
function fn(){
//声明
}
var fn = function () {
// 表达式
}
值类型var a = 100var b = aa = 200console.log(a) //100引用类型var a = { age:20}var b = ab.age = 21console.log(a.age) //21typeof 运行符typeof undefied //undefiedtypeof 'abc' //Stringtypeo...