You may see the dollar sign character ($) used in variable names, as in
$myvar or less commonly my$var. This character is allowed to appear
anywhere in a variable name, although previous versions of the ECMA
standard discouraged its use in handwritten programs and suggested
it should only be used in generated code (programs written by other
programs). This suggestion is not well respected by the JavaScript
community, and $ is in fact commonly used in practice as a function name.
变量名大小写敏感
变量作用域
If a variable is defined inside a function, it’s not visible outside of the function
The term “global variables” describes variables you define outside of any function (in the global program code), as opposed to “local variables”, which are defined inside a function.
if you don’t use var to declare a variable, this variable is automatically assigned a global scope.
任何类型,可以用两个逻辑否转换成boolean值,例如:var b = “one”; !!b == true
可以转换为 true 的值:
空字符串
null
undefined
0
‘NaN`
Undefined: undefined 。不存在的变量、声明后未赋值的变量。
Null: null。 It means no value, an empty value, or nothing. The difference with undefined is that if a variable has a value null, it’s still defined, it just so happens that its value is nothing.
if(a>2||a<-2){result='a is not between -2 and 2';}elseif(a===0&&b===0){result='both a and b are zeros';}elseif(a===b){result='a and b are equal';}else{result='I give up';}
functionHero(name){this.name=name;this.occupation='Ninja';this.whoAreYou=function(){return"I'm "+this.name+" and I'm a "+this.occupation;};}>varh1=newHero('Michelangelo');>varh2=newHero('Donatello');>h1.whoAreYou();"I'm Michelangelo and I'm a Ninja">h2.whoAreYou();"I'm Donatello and I'm a Ninja"
>newtoy.propertyIsEnumerable('name');true// Most built-in properties and methods are not enumerable.>newtoy.propertyIsEnumerable('constructor');falseAnypropertiescomingdowntheprototypechainarenotenumerable.>newtoy.propertyIsEnumerable('price');false// however, that such properties are enumerable if you reach the object contained// in the prototype and invoke its propertyIsEnumerable() method.>newtoy.constructor.prototype.propertyIsEnumerable('price');true
// trim() method for strings, which is a method // that exists in ES5 but is missing in older browsersif(typeofString.prototype.trim!=='function'){String.prototype.trim=function(){returnthis.replace(/^\s+|\s+$/g,'');};}>" hello ".trim();"hello"
functionShape(){this.name='Shape';this.toString=function(){returnthis.name;};}functionTwoDShape(){this.name='2D shape';}functionTriangle(side,height){this.name='Triangle';this.side=side;this.height=height;this.getArea=function(){returnthis.side*this.height/2;};}// prototype 链TwoDShape.prototype=newShape();Triangle.prototype=newTwoDShape();// reset the constructor after inheritingTwoDShape.prototype.constructor=TwoDShape;Triangle.prototype.constructor=Triangle;// 访问自己的方法>varmy=newTriangle(5,10);>my.getArea();25// 通过 prototype 链,访问到祖先类的方法>my.toString();"Triangle"// 重置了Prototype 的 constructor 后, constructor 是正确的>my.constructor===Triangle;true// instanceOf 操作符,可以识别出类型>myinstanceofShape;true>myinstanceofTwoDShape;true>myinstanceofTriangle;true>myinstanceofArray;false// The same happens when you call isPropertyOf()on the constructors passing my:>Shape.prototype.isPrototypeOf(my);true>TwoDShape.prototype.isPrototypeOf(my);true>Triangle.prototype.isPrototypeOf(my);true>String.prototype.isPrototypeOf(my);false// 父类的构造方法同样可以使用>vartd=newTwoDShape();>td.constructor===TwoDShape;true>td.toString();"2D shape">vars=newShape();>s.constructor===Shape;true
利用临时构造方法,实现继承 inheritence
this approach supports the idea that only properties and methods
added to the prototype should be inherited, and own properties should not.
// Shape 类functionShape(){}// augment prototypeShape.prototype.name='Shape';Shape.prototype.toString=function(){returnthis.name;};// TwoDShape 类functionTwoDShape(){}// take care of inheritancevarF=function(){};F.prototype=Shape.prototype;TwoDShape.prototype=newF();TwoDShape.prototype.constructor=TwoDShape;// augment prototypeTwoDShape.prototype.name='2D shape';// Triangle 类functionTriangle(side,height){this.side=side;this.height=height;}// take care of inheritancevarF=function(){};F.prototype=TwoDShape.prototype;Triangle.prototype=newF();Triangle.prototype.constructor=Triangle;// augment prototypeTriangle.prototype.name='Triangle';Triangle.prototype.getArea=function(){returnthis.side*this.height/2;};// ---- 开始测试>varmy=newTriangle(5,10);>my.getArea();25>my.toString();"Triangle"// 检查 Prototype 链>my.__proto__===Triangle.prototype;true>my.__proto__.constructor===Triangle;true>my.__proto__.__proto__===TwoDShape.prototype;true>my.__proto__.__proto__.__proto__.constructor===Shape;true// also the parents' properties are not overwritten by the children:>vars=newShape();>s.name;"Shape">"I am a "+newTwoDShape();// calling toString()"I am a 2D shape"
functionobjectPlus(o,stuff){varn;functionF(){}F.prototype=o;n=newF();n.uber=o;for(variinstuff){n[i]=stuff[i];}returnn;}// Create a 2D object by inheriting shape and adding more properties. vartwoDee=objectPlus(shape,{name:'2D shape',toString:function(){returnthis.uber.toString()+', '+this.name;}});// create a triangle object that inherits from 2D and adds more properties.vartriangle=objectPlus(twoDee,{name:'Triangle',getArea:function(){returnthis.side*this.height/2;},side:0,height:0});// creating a concrete triangle my with defined side and heightvarmy=objectPlus(triangle,{side:4,height:4});>my.getArea();8>my.toString();"Shape, 2D shape, Triangle, Triangle"
vartwoD={name:'2D shape',dimensions:2};functiontriangle(s,h){varthat=object(twoD);that.name='Triangle';that.getArea=function(){returnthis.side*this.height/2;};that.side=s;that.height=h;returnthat;}// Because triangle() is a normal function, not a constructor, it doesn't require the newoperator.>vart=triangle(5,10);>t.dimensions;2// But because it returns an object, calling it with new by mistake works too.>vart2=newtriangle(5,5);>t2.getArea();12.5
借用父类构造器的继承 borrowing a constructor
This can be called stealing a constructor, or inheritance by
borrowing a constructor
functionShape(id){this.id=id;}Shape.prototype.name='Shape';Shape.prototype.toString=function(){returnthis.name;};functionTriangle(){// 利用 Object.apply 借用 Shape's constructorShape.apply(this,arguments);}Triangle.prototype.name='Triangle';>vart=newTriangle(101);>t.name;"Triangle">t.id;101// The triangle failed to get the Shape function's prototype properties // because there was never a new Shape() instance created, so the prototype was never used. >t.toString();"[object Object]"// ---// 利用 prototype 继承functionTriangle(){Shape.apply(this,arguments);}Triangle.prototype=newShape();Triangle.prototype.name='Triangle';
functionShape(id){this.id=id;}Shape.prototype.name='Shape';Shape.prototype.toString=function(){returnthis.name;};functionTriangle(){Shape.apply(this,arguments);}extend2(Triangle,Shape);Triangle.prototype.name='Triangle';// Testing>vart=newTriangle(101);>t.toString();"Triangle">t.id;101// No double inheritance:>typeoft.__proto__.id;"undefined"
>varurl='http://www.packtpub.com/script.php?q=this and that';>encodeURI(url);"http://www.packtpub.com/script.php?q=this%20and%20that">encodeURIComponent(url);"http%3A%2F%2Fwww.packtpub.com%2Fscr%20ipt.php%3Fq%3Dthis%20and%20that"