What does "instance" mean in javascript?

Answer to this question: What is the initial value of the prototype property of a JavaScript function?

has the following sentence:

The initial prototype value for any newly created instance of the function is a new instance of Object

As far as I know, Javascript has no classes, and the word "instance" for this does not make sense in my head. How to interpret "instance" in Javascript?

Sorry, I do not have enough answers to put my question in the comment stream for this answer.

+8
javascript function object instance
source share
7 answers

You are right that JavaScript does not have classes (yet), but it does have design functions, an instanceof operator that defines the relationship between objects and constructors, and an inheritance form based on prototype chains.

obj instanceof ctor true when ctor.prototype is in the obj prototype chain.

By changing the caution below, you can implement instanceof in EcmaScript 5 in this way

 function isInstanceOf(obj, ctor) { var proto = ctor.prototype; if (typeof obj === "object" || typeof obj === "function") { while (obj) { if (obj === proto) { return true; } obj = Object.getPrototypeOf(obj); } } return false; } 

If you are not going to reassign the prototypes ( o = new MyConstructor(); MyConstructor.prototype = somethingElse ), it should be like new MyConstructor() instanceof MyConstructor .

Section 15.3.5.3 explains this in detail.

15.3.5.3 [[HasInstance]] (V)

Suppose F is a Function object.

When the [[HasInstance]] F internal method is called with a value of V, the following steps are performed:

  • If V is not an object, return false.
  • Let O be the result of calling the [[Get]] F internal method with the property name "prototype".
  • If Type (O) is not an object, throw a TypeError exception.
  • Repeat

    • Let V be the value of the [[Prototype]] internal property of V.
    • If V is null, return false.
    • If O and V refer to the same object, return true.

This is not the whole story, because host objects (such as DOM nodes) are allowed to implement the [[HasInstance]] internal method, but they prefer, but most browsers implement host objects so that they are as close to their native objects as possible.

+16
source share

JavaScript has no classes, but it has types. You can define your own type and create a new instance using the new keyword:

 function Foo(initialValue) { this.value = initialValue || 0; } var foo = new Foo(17); var fooIsAFoo = foo instanceof Foo; // true! var fooIsAnObject = foo instanceof Object; // also true! We have inheritance here :) 
+5
source share

In JS, a function is an object. It takes time to get used to it, but it is true. Throw it a little, and the code that you see others will make sense. If you saw Javascript where people pass the function () {} as a parameter to another function, this is proof that the functions are first class objects.

Once you can wrap yourself around (it took me a while), you need to understand that if you create a function, you can get a new instance. See the following code:

 function FooName(){ var name; this.setName = function(n){ this.name = n; } this.getName = function(){ return this.name; } } var n1 = new FooName(); var n2 = new FooName(); n1.setName("FOO"); n2.setName("BAR"); 

At this point, you have two examples of the FooName method: n1 and n2. This is a kind of convention in JS to call your effective function the first letter of the top level, and not the lower case letter of the client. In this example, I indicate that my function can be set by naming FooName instead of fooName. I hope this makes sense.

In the above example, n1 has three properties. The private name property, the public setName property, and the public getName property. Each time I create a new FooName, a copy of getName and setName will be created. This may lose memory space. Since getName and setName will not change, I do not need a new copy for each instance of FooName. Prototypes appear here.

You can say the following, and then getName and setName will exist only once in memory, freeing memory, which is good.

 FooName.prototype.setName = function(n){ this.name = n; } FooName.prototype.getName = function(){ return this.name; } 

If you remove getName and setName from the FooName function, now you will have FooName "class", which has two methods: getName and setName.

Play with him. Let me know if you have any questions.

+3
source share

JavaScript functions have first-class objects, which means that you can handle them just like any other object.

// Define class

  function User(name,age) { this.Name=name; this.Age=age } 

// create insatnces

 var user1= new User('Demo1','50'); var user2= new User('Demo2','100'); alert(user1.Name); alert(user2.Name); 

http://jsfiddle.net/praveen_prasad/hmUku/

http://en.wikipedia.org/wiki/First-class_function

+1
source share

Javascript does not use classical inheritance, it uses prototype inheritance , where everything is an “object”, and inheritance is done by cloning, for example

 var myPrototype = function() { var privateMember = "I am private"; var publicMember = "I am public"; var publicMethod = function () { alert(privateMember); } return { publicMember = publicMember, publicMethod = publicMethod } } var myInstace = new myPrototype(); myInstance.publicMethod(); 

here myInstance can be called an "instance" of myPrototype , but in fact it happened that with the new keyword you cloned myPrototype to create myInstance .

+1
source share

So now I have fabricated a page describing what I think all of you guys are saying: https://docs.google.com/document/d/1VvqqO2LMmilLbIvcU2JHI9ifaoMs-DB-farEzwuVuwM/edit?hl=da

illustrated with nice graphics :)

thinks jon

* and this link: http://trephine.org/t/index.php?title=Understanding_the_JavaScript_new_keyword

0
source share

Javascript is object oriented, so it has classes. There simply is no explicit "class" directive, as in other OOP languages.

-2
source share

All Articles