make it look like java
(function() {
'use strict';
...
}());
var fred = {};
Object.defineProperty(fred, "name", {
get: function() {
console.log("Retrieving name: " + name);
return name;
},
set: function(value) {
console.log("Setting name to: " + value);
name = value;
}
});
var Button = function(content) { this.content = content; };
Button.prototype.click = function() {
console.log(this.content + ' clicked');
}
var fred = new Button('OK');
var boundClick = fred.click.bind(fred);
boundClick();
var fred = {
firstName: "Fred",
lastName: "Flinstone",
hometown: "Bedrock",
wife: {
firstName: "Wilma",
lastName: "Flinstone"
}
};
arguments
array
var myObject = (function () {
var value = 0;
return {
increment: function (inc) {
value += typeof inc === 'number' ? inc : 1;
},
getValue: function ( ) {
return value;
}
};
}());
source: JavaScript: The Good Parts, Douglas Crockford
new
operator
Object.create
var fred = {
name : 'Fred Flinstone',
saying : 'Yabadabadoo!',
says : function() { return this.saying || ''; }
};
var barney = Object.create(fred);
barney.name = 'Barney Rubble';
barney.saying = 'Hi Fred';
for
iso
for in
return
{
status: true
};
↓
return;
{
status: true
};
Value | Type |
---|---|
0
|
Number |
NaN
|
Number |
''
|
String |
false
|
Boolean |
null
|
Object |
undefined
|
Undefined |
==
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
' \t\r\n ' == 0 // true