Is an object instance really just inheriting somehow in javascript?

I read the JavaScript book, and when I read the chapter on inheritance, I wondered if every time you create an instance of an object, it really is inheritance, especially since they are similar.

Examples: Prototype Chain - someObject.prototype = new someOtherObject();

Object Creation var myArray = new Array()

Similar right?

In addition, in the book (JavaScript for Web Developers by Nicholas Zakas), there is a link between the instance and the prototype constructor. (The link is usually called __proto__)

Is it possible to claim that an instance of an object is similar to inheritance?

+3
source share
2 answers

. , ( ) . - .

, someObject.prototype = new SomeOtherObject().

someObject.prototype = { myObjectLiteral: "isJustAsGoodAsYourConstructoredInstance" };

someObject.prototype = false. , , .


EDIT OP, :

" , : , ."

, , . , " -, ". , , ,

Array.prototype === Array
Function.prototype === Function

function CustomConstructor() { }
CustomConstructor.prototype === CustomConstructor

false, .

, " ". , , . :

var myProto = {};

function CustomConstructor() { }
CustomConstructor.prototype = myProto; // inherit from the myProto object.

var x = new CustomConstructor(); // x is an "instance"
Object.getPrototypeOf(x) === myProto; // Object.getPrototypeOf fetches the "internal pointer"

, , , . . prototype , , extends BaseClass Java. , , , .


JavaScript , , . "", "" " ".

+2

objectType.inherits(), , :

new someOtherObject()

someOjectObject.

, someOtherObject, ( ). {}.

someObject:

someObject.prototype = new someOtherObject();

, someObject, :

someObject, someOtherObject , () , .

, , , . (), proto . , , javascript ( ), , . , , .

+1

All Articles