Zenitram.PRO
Zenitram.PRO
Reputation Top 1%
Paul Martinez
16 Snippets  (49th place)
Published
2 Channels
Created
3 Channels
Following
4953 points  (21st place)
Reputation
Top 10% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Junior Publisher
Serious Publisher
Senior Publisher
Junior Topic Creator
Junior Topic Hub
Junior Trend Maker
Serious Trend Maker
Junior Judge
Serious Judge
Junior Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Senior Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by Zenitram.PRO

public by Zenitram.PRO  3599  1  5  1

How to convert retrieved text in JSON format containing line breaks.

Using $.ajax, dataType: "JSON".
function parseline($string){
  $string = str_replace(chr(10), "//n", $string);
  $string = str_replace(chr(13), "//n", $string);
  return $string;
} 
;

public by Zenitram.PRO  3286  4  5  1

How to Inherit another Class' Properties using Prototyping.

DRY Penguins Creating a brand new Penguin was nice, but we did end up reusing a lot of the same code as the Animal class. This goes against the "DRY" principle of programming: Don't Repeat Yourself. Inheritance can help us here! A Penguin is an Animal, so they should have all the same properties and methods as Animal. Whenever this X is-a Y r
// the original Animal class and sayName method
function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};

// define a Penguin class
function Penguin(name) {  
    this.name = name;
    this.numLegs = 2;
}

// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();
             
;

public by Zenitram.PRO  3521  1  5  1

How to use Prototype in Object

It's All in the Genes In object-oriented programming, inheritance allows one class to see and use the methods and properties of another class. You can think of it as a child being able to use his or her parent's money because the child inherits the money. We will learn more about inheritance as we continue this lesson, but for now let's just
// create your Animal class here
function Animal(name,numLegs)
{
    this.name = name;
    this.numLegs = numLegs;
}


// create the sayName method for Animal
Animal.prototype.sayName = function () {
    console.log("Hi my name is " + this.name);
} 

// provided code to test above constructor and method
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();            
;

public by Zenitram.PRO  404435  3  5  1

Adding an Element in an Array using a Function.

Arrays of Objects Remember that an object is just another type, like a string or number but more complex. This means that just as we can make arrays of numbers and strings, we can also make arrays of objects. Here we have our Person constructor which should look familiar. We can use this constructor to make an array of Person objects, similar
// Our person constructor
function Person (name, age) {
    this.name = name;
    this.age = age;
}

// Now we can make an array of people
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
// add the last family member, "timmy", who is 6 years old
family[3] = new Person("timmy", 6);
                                                
;

public by Zenitram.PRO  4137  3  6  1

Creating new Object Using a pre-defined Function(Custom Constructor) 2.

Constructors in Review Constructors are a way to make objects with the keyword new. The most basic constructor is the Object constructor, which will make an object with no methods or properties. For more complicated objects we can make our own constructors and put in whatever properties and methods we want. Check out our example to the rig
function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
    };
}

// now we can easily make all of our rabbits
var rabbit1 = new Rabbit("fluffy");
var rabbit2 = new Rabbit("happy");
var rabbit3 = new Rabbit("sleepy"); 

rabbit1.describeMyself();
rabbit2.describeMyself();
rabbit3.describeMyself();                                                                                                
;