mirror of
https://github.com/flynx/Course-JavaScript.git
synced 2025-11-02 21:10:08 +00:00
Compare commits
No commits in common. "98eedb02bc84ea46922d73225d6767ea241ab341" and "8209790c8d02e41e5687ae70615577b1109a68b5" have entirely different histories.
98eedb02bc
...
8209790c8d
@ -119,7 +119,6 @@
|
|||||||
|
|
||||||
// Prototypes and inheritance
|
// Prototypes and inheritance
|
||||||
//
|
//
|
||||||
// XXX
|
|
||||||
|
|
||||||
var a = {
|
var a = {
|
||||||
}
|
}
|
||||||
@ -132,14 +131,6 @@
|
|||||||
|
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
//
|
|
||||||
// A constructor is simply a function that "constructs" or populates an
|
|
||||||
// object.
|
|
||||||
//
|
|
||||||
// By convention constructor functions are capitalized (Pascal-case)
|
|
||||||
//
|
|
||||||
// Classic constructors are called with a "new" keyword which creates a
|
|
||||||
// bare instance and passes it to the function as the call context.
|
|
||||||
//
|
//
|
||||||
|
|
||||||
function A(){
|
function A(){
|
||||||
@ -155,12 +146,7 @@
|
|||||||
__proto__: A.prototype,
|
__proto__: A.prototype,
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
// XXX a safer way -- now we can forget new...
|
||||||
// The problem with the default way this is done is that now a
|
|
||||||
// constructor will behave differently when called directly or if called
|
|
||||||
// via the new syntax. This can be desirable in some cases but in
|
|
||||||
// general this is a pitfall, so let's unify the two cases:
|
|
||||||
//
|
|
||||||
|
|
||||||
function B(){
|
function B(){
|
||||||
var obj = {
|
var obj = {
|
||||||
@ -169,10 +155,10 @@
|
|||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
|
|
||||||
// this can be called with and without new:
|
// this can be calles with and withot new
|
||||||
var z = B()
|
var z = B()
|
||||||
|
|
||||||
// less naive -- reuses the instance created by new...
|
// less naive...
|
||||||
function C(){
|
function C(){
|
||||||
var obj = this instanceof C ?
|
var obj = this instanceof C ?
|
||||||
this
|
this
|
||||||
@ -182,11 +168,6 @@
|
|||||||
// make C instances related to B...
|
// make C instances related to B...
|
||||||
C.prototype.__proto__ = B.prototype
|
C.prototype.__proto__ = B.prototype
|
||||||
|
|
||||||
//
|
|
||||||
// Note that constructor extension is trivial if you think of how
|
|
||||||
// prototypical inheritance works, to link A and B "instances" all we
|
|
||||||
// needed to do is link the constructor prototypes in the code above.
|
|
||||||
//
|
|
||||||
|
|
||||||
|
|
||||||
// Extending builtin types
|
// Extending builtin types
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user