Thanks for you help on this one.
See reply on bottom. It works now for the function constructor and now I can add as many names as I want just using the “new” operator and they will all share the same prototypes.
<DOC Type html>
<html>
<head>
<title>Function Constructors, "new" and Setting New Prototypes </title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src = "https://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script>
//Hello Everyone lets do some Javascript
console.log("Hello World");
//Copy and paste this post to find out how to build Objects using function contructors and the 'new' property
//Building Objects , Function Constructiors, 'new' and how to Set New Prototypes.
//A function constructor is a normal function that is used to construct objects. Lets take a look at these examples.
function Person(firstname,lastname){
console.log(this);
this.firstname = firstname;
this.lastname = lastname;
console.log('This function is invoked');
return{greeting: 'I got in the way'}
}
//I can now create/construct a new Object using the "new" operator. So I can create these OBjects on the fly and they will have the same prototypes as the original Object used to create it except it will first create an empty Object
var mike = new Person();
console.log(mike); //firstname and lastname undefined but same prototypes as original for both new Persons
var sarah = new Person();
console.log(sarah);
// Next we will construct the new object we can also change the parameters of the function on the fly like firstname and lastname can be changed to the real first and lastnames for every Object / new Persons that you create.
var john = new Person('John', 'Doe'); //names defined
console.log(john);
var jane = new Person('Jane' , 'Doe'); //names defined
console.log(jane);
// Next we will do the same thing as before except this time when we can also add some more prototypes to the object on the fly and they will become prototypes of the new Object as will.
function Persons(firstname,lastname){
console.log(this);
this.firstname = firstname;
this.lastname = lastname;
console.log('This function is invoked now');
}
Persons.prototype.getFullName = function(){
return this.firstname + " " + this.lastname;
};//new prototype added on the fly to all others
var johnny = new Persons('John', 'Lewis');
console.log(johnny);
var janet = new Persons('Janet', 'Jackson');
console.log(janet);
Persons.prototype.getFormalName = function(){
return this.lastname + " " + this.firstname;
} //new prototype added on the fly to all others
console.log(janet.getFormalName());
console.log(johnny.getFullName());
console.log(Persons.prototype); //list Object and all prototypes
//The 'this' variable points to a new empty Object { }, and that object is returned from the function automatically. The 'new' operation creates or constructs the new Object with all the prototypes of the original Object. This is how do you set the protoype when constructing a new Object? Javascript does this automatically. But you can also all prototypes to newly created objects or to the originall Object that it was created from. This is powerful because that means that I can also add prototypes to existing default Objects like numbers and strings and booleans and arrays. This is how JQuery is developed and other JS libraries.
//Built-in Function Constructors in Javascript
// There are many built-in function constructors in Java script for instance new Number creates an Object that holds a primitive value but the Number is an Object and it has several prototypes that are associated with it. Play with this int he console.
var a = new Number(3); // construct Object primitive value(3)
console.log(a); //will print Object with primitive value
Number.prototype; //All the Number prototypes
a.toFixed(3); // 3.000
var b = new Date("1/3/1959");
console.log(b);
b.prototype; //Date prototypes
var c = new String("John"); //Creates Object String
c.prototype //will list prototypes of String
console.log(c);
console.log(c.length);
//So what you thing are primitive value are actually Object that contain primitive values. And because they are Objects they have prototypes and methods.
</script>
</body>
</html>