Posts

Showing posts with the label Javascript

Why wrapping in the jQuery object

So here's my answer: $(function() { $('img').attr('src', 'http://placepuppy.it/350/150'); }) I'm simply starting with the jQuery object and passing it an anonymous function. The anonymous function changes the  src  of the one  img  on the page to the URL provided. (Remember, $('img')  grabs  all  of the images on the page, so this is a  very bad  selector. It works in this case because there's only one  , but normally you should use a much more specific selector.) If I hadn't wrapped my  .attr()  function in the jQuery object, it would run as soon as it's loaded in the  of the document, which occurs before the   tag appears on the page. So nothing would happen. But by wrapping it up in the jQuery object, it runs when the DOM is ready and I get to see a cute puppy instead! Source 

Literal notation vs Dot notation in Javascript

var myArray =[a:"book", b: "paper",c: "pen"]; myArray [a]; This is Literal Notation and gives us the value  "book". myArray.a; This is Literal Notation and gives us the value "book" Literal notation in object var student= { total: 0 }; Dot notation in object student.total = 0;

Class and Object in Javascript

Class function Cat(name, age) { this.name = name; this.age= age; } In above it is cat class Object var cat= new cat("remi", 4); In above it is cat object

How to Print Out All the property/propert value of Object in Javascript

First Define an Object with property and then use for /in loop with console to print out Example: Defining Object with property var fifa = { fullName: " International Federation of Association Football ", president: " Sepp Blatter", motto: " For the Game. For the World ", formation: " 21 May 1904 (110 years ago) " };  Now print out property  using for /in loop with console for (var property in fifa){ console.log(property); }  Now print out property value  using for /in loop with console for (var key in fifa){ console.log(fifa[key]); }

Function with Method in JavaScript

Example function Class (){ this . method = function (){ alert ( 'simple method' )}; }​​​​​​​​​ var object = new Class (); object . method ();​ Example with Method Speak and function person  function Person() { this.speak =function (){ console.log ("Hello!"); } } var user = new Person(); user.speak();   Here speak is a method of Person function