jobo: OOP in Javascript - Object prototypisch erweitern

Beitrag lesen

Hallo,

protected geht gar nicht mehr.

Hm, kommt das nicht wirklich darauf an, wo man die Variablen definiert?

myObj = {
method1 : function () {
var myPrivate = "privat1";
return myPrivate;
}
}
myFunction = function (callback) {
alert(callback());
}
myFunction(myObj.method1);

function object(o) {
function F() {}
F.prototype = o;
return new F();
}
var myExtendedObj = object(myObj);
myFunction(myExtendedObj.method1);


>   

Besser so:  
  
~~~javascript
  
myObj = {  
	method1 : function () {  
		var myPrivate = "privat1";  
		return myPrivate;  
	}  
}  
myFunction = function (callback) {  
	alert(callback());  
}  
myFunction(myObj.method1);  
/*  
Crockford 2008-04-07 (http://javascript.crockford.com/prototypal.html):  
"The problem with the object function is that it is global, and globals are clearly problematic.  
The problem with Object.prototype.begetObject is that it trips up incompetent programs,  
and it can produce unexpected results when begetObject is overridden.  
So I now prefer this formulation:"  
*/  
if (typeof Object.create !== 'function') {  
    Object.create = function (o) {  
        function F() {}  
        F.prototype = o;  
        return new F();  
    };  
}  
var myExtendedObj  = Object.create(myObj);  
myFunction(myExtendedObj.method1);  

"For convenience, we can create functions which will call the object function for us, and provide other customizations such as augmenting the new objects with privileged functions. I sometimes call these maker functions. If we have a maker function that calls another maker function instead of calling the object function, then we have a parasitic inheritance pattern.

I have found that by using these tools, coupled with JavaScript's lambdas and object quasi-literals, I can write well-structured programs that are large, complex, and efficient. The classical object model is by far the most popular today, but I think that the prototypal object model is more capable and offers more expressive power.

Learning these new patterns also made me a better classical programmer. Insights from the dynamic world can have application in the static."

Crockford

Gruß

jobo

0 112

public protected and private in javascript

Florian Bücklers
  • javascript
  1. 0
    Struppi
    1. 0
      jobo
      1. 0
        jobo
      2. 0
        Florian Bücklers
        1. 1

          Standortbestimmung: Programmierparadigmen, OOP, OOP in JS ...

          peterS.
          1. 0
            Florian Bückkers
            1. 0
              peterS.
              1. 0
                jobo
              2. 0
                Florian Bücklers
              3. 2
                peterS.
                1. 0
                  peterS.
            2. 3
              molily
      3. 0
        molily
        1. 0
          jobo
  2. 1
    Struppi
    1. 0
      Florian Bücklers
  3. 2
    molily
    1. 0

      javascripts oop und ein beispiel

      jobo
      1. 0
        Struppi
    2. 1
      Florian Bücklers
      1. 1
        molily
        1. 0
          Struppi
          1. 1
            molily
            1. 0
              Struppi
              1. 0
                jobo
                1. 0
                  Struppi
              2. 1
                molily
                1. 0
                  Struppi
                  1. 0
                    molily
                    1. 0
                      Struppi
                      1. 0
                        jobo
                        1. 0
                          Struppi
                          1. 0
                            jobo
                          2. 1
                            molily
                            1. 0
                              jobo
                              1. 0
                                jobo
                  2. 1
                    molily
                2. 0
                  Florian Bücklers
                  1. 0
                    jobo
                    1. 0
                      Struppi
                      1. 0
                        jobo
                        1. 0
                          Struppi
                          1. 0
                            jobo
                            1. 0

                              was ist OOP und gibt es eine Implementation in Ecmascript

                              jobo
                            2. 0

                              OOP in Javascript - Object prototypisch erweitern

                              jobo
                            3. 0
                              Struppi
                              1. 0
                                jobo
                                1. 0

                                  protected und Javascript - übergeordnetes (Sicherheits-)Konzept

                                  jobo
                                2. 0
                                  Struppi
                                  1. 0
                                    jobo
                                    1. 0

                                      Diskussionszusammenfassung auf Wikipedia

                                      jobo
                                      1. 0
                                        Struppi
                                        1. 2
                                          molily
                                          1. 0

                                            Diskussionszusammenfassung von molily und node.js

                                            jobo
                                            1. 0
                                              Struppi
                                              1. 0

                                                der Punkt um den es geht ... Privatheitskonzept von Javascript

                                                jobo
                                                1. 0
                                                  Struppi
                                                  1. 0
                                                    jobo
                                                  2. 0

                                                    serverseitiges Javascript - node.js

                                                    jobo
                                                    1. 0
                                                      peterS.
                                                  3. 0

                                                    mitlesender

                                                    peterS.
                                                    • menschelei
                                                    1. 0
                                                      JürgenB
                                            2. 0
                                              molily
                                              1. 0

                                                Diskussionszusammenfassung von molily, node.js und loops

                                                jobo
                      2. 2
                        molily
                        1. 0
                          Struppi
                          1. 0

                            Privatheitskonzept in Javascript

                            jobo
                            1. 0
                              Struppi
                          2. 0
                            molily
                            1. 1
                              molily
                          3. 0
                            jobo
                            1. 0

                              Codereuse und DesignPatterns OOP ohne Vererbung und POO

                              jobo
                              1. 0
                                JürgenB
                                1. 0
                                  jobo
                          4. 0
                            Don P
                            1. 0

                              privat und (pseudo-)"Klassen" - Prototype und Scopevariablen

                              jobo
                              1. 0
                                Don P
                                1. 0
                                  Struppi
                                  1. 0
                                    Don P
                                    1. 0
                                      Struppi
                                      1. 0
                                        Don P
                                        1. 0
                                          Struppi
                                          1. 0

                                            Konstruktor-Closure vs. Prototyp

                                            molily
                                            1. 0
                                              jobo
                                          2. 0
                                            jobo
                                            1. 0
                                              jobo
                                            2. 0
                                              Struppi
                                              1. 0
                                                jobo
                                                1. 0

                                                  Crockfords "module pattern" - unabhängig von YUI(YAHOO)

                                                  jobo
                                                2. 0
                                                  peterS.
                                                  1. 0
                                                    jobo
                                                    1. 0
                                                      Struppi
                                                      1. 0
                                                        jobo
                                                        1. 0
                                                          Struppi
                                                          1. 0

                                                            Performance und "this" im Kontruktor

                                                            jobo
                                                            1. 0
                                                              Struppi
                                                              1. 0
                                                                jobo
                                                                1. 0
                                                                  Struppi
                                                                  1. 0
                                                                    jobo
                                                  2. 0
                                                    Don P
                                                    1. 0

                                                      private mit "revealing module patter" und "speaking code"

                                                      jobo
                                                    2. 0
                                                      Struppi
                                    2. 0
                                      jobo
                                      1. 0
                                        Struppi
                                        1. 0
                                          jobo
                            2. 0
                              Struppi
                  2. 1
                    molily
            2. 0
              jobo
              1. 2
                molily
              2. 1
                molily
                1. 0
                  jobo