The 'New' Keyword in JavaScript
- 5. var simpleObject = {
a: 'a'
};
{
a: 'a'
}
simpleObject.toString(); // um, where does this function come from?
- 7. var simpleObject = {
a: 'a'
};
{
a: 'a',
__proto__: {
...
toString: function,
hasOwnProperty: function,
...
}
}
- 8. var Demo = function() {
...
};
Demo.prototype.doSomething = function() {
...
};
- 9. var Demo = function() {
this.prop = 'text';
};
Demo.prototype.action = function() {
alert(this.prop);
};
var myDemo = Demo();
myDemo; // undefined
myDemo.action() // ERROR
window.prop; // 'text'
- 10. var Demo = function() {
this.prop = 'text';
};
Demo.prototype.action = function() {
alert(this.prop);
};
var myDemo = Demo();
myDemo; // { prop:'a', action:function }
window.prop; // undefined
myDemo.action(); // alerts "text"
- 11. var Demo = function() {
this.prop = 'text';
};
Demo.prototype.action = function() {
alert(this.prop);
};
var myDemo = new Demo();
- 12. var Demo = function() {
this.prop = 'text';
};
Demo.prototype.action = function() {
alert(this.prop);
};
var myDemo = new Demo();
- 15. var Demo = function() {
this.prop = 'text';
};
Demo.prototype.action = function() {
alert(this.prop);
};
var myDemo = new Demo();
var Demo = {
init: function() {
this.prop = 'text';
},
action: function() {
alert(this.prop);
}
};
var myDemo = Object.create(Demo);
myDemo.init();