I don't get it. Whats with the for loop part? 30.Private Eye


#1

function StudentReport() {
this.grade1 = 4;
this.grade2 = 2;
this.grade3 = 1;
var getGPA = function() {
return (this.grade1 + this.grade2 + this.grade3) / 3;
};
}

var myStudentReport = new StudentReport();

for(var x in myStudentReport) {
if(typeof myStudentReport[x] !== "function") {
console.log("Muahaha! " + myStudentReport[x]);
}
}

console.log("Your overall GPA is " + myStudentReport.getGPA());

What is the for loop saying? I think its saying if typeof myStudentReport is not a function then print to the console, which would single out the gpa method. And since we turn "this" into "var" for the grade1, grade2, and grade3, it is now private and therefore cannot appear outside of this Object. But the fact that it still calculates the getGPA method is not sitting right with me. I think I've been coding for too many hours today. I'm not getting it.


#2

@killerg,

//Defining an Object Class constructor
//using a named-function
function privatePublic() {
    this.publicly = 10;
    var privately = 1010;
}

Creating a so-called Instance of this Object Class of objects…

var testInstance = new privatePublic();
//console.log() will =display= publicly-accessable-properties
console.log( testInstance );
//[output] { publicly: 10 }

Now we are going to modify a =publicly-accessable= property testInstance object.

//Modifying publicly accessable poperty
testInstance.publicly = 11;
console.log( testInstance );
//[output] { publicly: 11 }

Now the trial to change a =private= property of the testInstance object using try {} catch{}

//Modifying =private= property from outside of Instance, not possible
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
try {
 testInstance.privately = 1011;
 //throw "privatelyTest";
 //throw e;
 throw new Error();
}
catch (e) { 
    console.log( e );
    if (e instanceof TypeError) {
    // statements to handle TypeError exceptions
       console.log( "Produced a TypeError");
    }
    else if (e instanceof RangeError) {
    // statements to handle RangeError exceptions
       console.log( "Produced a RangeError");
    }
    else if (e instanceof EvalError) {
    // statements to handle EvalError exceptions
       console.log( "Produced a EvalError");
    }
    else  {
       console.log( "Could not update =privately= property" );
       console.log("This is your try-control " );
       console.log( "== e ==" );
       console.log( e );
       console.log( "\n== e.number ==" );
       console.log( e.number );
       console.log( "\n== e.description ==" );
       console.log( e.description );
       console.log( "\n== e.stack ==" );
       console.log( e.stack );
       
    }
}

A =run= on the code will produce

/* [output]
{ publicly: 10 }
{ publicly: 11 }
{ number: 0, description: '', stack: [Getter/Setter] }
Could not update =privately= property
This is your try-control 
== e ==
{ number: 0, description: '', stack: [Getter/Setter] }

== e.number ==
0

== e.description ==


== e.stack ==
Error
   at eval code (eval code:24:2)
   at a.prototype.Eval (eval code:1:272)
   at d[a] (.../jsrepl/sandbox.js:42:130)
   at Anonymous function (.../jsrepl/sandbox.js:37:209)
   
*/

##We will need to add a GETTER Method and SETTER Method
##to be able
##to access/modify the =private= properties of the instanceTest Object


#3

@killerg,

//using a named-function
function privatePublic() {
    this.publicly = 10;
    var privately = 1010;
    this.setter = function( newValue ){
        console.log( "== setter executing ==" );
        console.log( "argument  received: " + newValue);
        console.log( "privately oldValue: " + privately);
        privately = newValue;
        console.log( "privately newValue: " + privately);
    };
    this.getter = function( ) {
        console.log( "== getter executing ==" );
        console.log( "argument  received: " + "None");
        console.log( "privately Value: " + privately);
        console.log( "publicly  Value: " + this.publicly);
        return privately;
    };

}

//

var testInstance = new privatePublic();
//console.log() will =display= publicly-accessable-properties
console.log( "Publicly-accessable properties of testInstance" );
console.log( testInstance );
console.log( "== testInstance's setter property ==" );
console.log( testInstance.setter.toString() );
console.log( "== testInstance's getter property ==" );
console.log( testInstance.getter.toString() );
//[output] 
testInstance.setter(1011);
testInstance.getter("privately");
//Modifying publicly accessable poperty
testInstance.publicly = 11;
console.log( testInstance );
//[output] { publicly: 11, setter: [Function], getter: [Function] }

The output in the console

Publicly-accessable properties of testInstance
{ publicly: 10, setter: [Function], getter: [Function] }
== testInstance's setter property ==
function( newValue ){
        console.log( "== setter executing ==" );
        console.log( "argument  received: " + newValue);
        console.log( "privately oldValue: " + privately);
        privately = newValue;
        console.log( "privately newValue: " + privately);
    }
== testInstance's getter property ==
function( ) {
        console.log( "== getter executing ==" );
        console.log( "argument  received: " + "None");
        console.log( "privately Value: " + privately);
        console.log( "publicly  Value: " + this.publicly);
        return privately;
    }
== setter executing ==
argument  received: 1011
privately oldValue: 1010
privately newValue: 1011
== getter executing ==
argument  received: None
privately Value: 1011
publicly  Value: 10
{ publicly: 11, setter: [Function], getter: [Function] }
nice to know

You could test in labs repl.it
choose Javascript
You will get a split-screen
In the left-hand-side you paste-in your code
click on the Run button.
The right-hand-side is the console

references

www.crockford.com --> for most respected guide-line in Javascript
like coding convention
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html


#4

@killerg,

+++++ nyc object and the for-in loop explained ++++++++++++++++

T h e - B a s i c s

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,
- a fullName property with property-key fullName and it's associated string value of "New York City"
- a mayor property with property-key mayor and it's associated string value of "Bill de Blasio"
- a population property with property-key population and it's associated number value 8000000
- a boroughs property with property-key boroughs and it's associated number value 5

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration's
iteration-1 var x = "fullName";
iteration-2 var x = "mayor";
iteration-3 var x = "population";
iteration-4 var x = "boroughs";
As they want you to display the property-key and NOT it's associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );