Giter Club home page Giter Club logo

Comments (4)

douglasquade avatar douglasquade commented on August 24, 2024

Seems that it does not work when using Object.create() new javascript implementation.

It works when creating objects in this way:

function myobject() {
this.container = new CAAT.Actor();
};

Any ideas?

from caat.

karlwestin avatar karlwestin commented on August 24, 2024

This has nothing to do with CAAT, but with javascripts prototypal inheritance.

I'll try an attempt to explain this:
First look at this jsbin (your example-code)

Why do obj1.container and obj2.container all of a sudden have the same id?

When objects are created with Object.create, the original object is added as the prototype to the new object. Since all objects and array are passed by reference in javascript, that means that the actor in original object (testing) and the actor of the copy (for example obj1) are actually pointing to the same instance of CAAT.Actor

When you create an actor using the new keyword, a new object ("this") is created, and the properties that are added in the CAAT.Actor constructor are added. Finally the new "this" object is returned.

What does this mean in practical use?

I think i will confuse you even more now, but this works perfectly fine – Why?
Because when i call setId on for example obj1, what happens behind the scenes is
this.id = "obj1" //"this" refers to obj1
in your example, what happened was
this.id = "obj1" //"this" refers to obj1.prototype, which is shared by all three

Anyway i hope this cleared things out a little, I also recommend the Inheritance chapter in "Pro Javascript Design Patterns" by Dustin Diaz and Ross Harmes if you'd like to know more about this!

from caat.

douglasquade avatar douglasquade commented on August 24, 2024

Thanks for your help.

Maybe it's not a good idea to use this pattern.

I will take a look at that book

from caat.

hyperandroid avatar hyperandroid commented on August 24, 2024

Non CAAT related questions must be asked in the forums at http://groups.google.com/group/caatjs?hl=es
Thanks for answering Karl.
Thanks.

from caat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.