Digest cycle in angularjs

Digest cycle in angularjs is very same as any digestion process. As angularjs allows two way data binding, the change in model has to be applied to views or vice versa.

When an event is triggered which changes the model, angularjs initiates the digest cycle.

every time we bind data to view, an watcher is being initiated behind the scene to verify the changes which is similar to $watch. The digest cycle triggers the watchers which are continuously verifying the change in the model.

digest cycle runs maximum up to 10 times to compare the old value and the new value. It stops when they are same. Digest cycle starts at the $rootScope and propagates to each nested scope.

When you are using a JavaScript inbuilt function like setInterval instead of $interval to update the model then angularjs does not understand to trigger the digest cycle. That time we need to manually call the $scope.$apply() method to initiate the digest cycle.

Advertisements

Enumerable in JavaScript

Object.defineProperty(obj, 'answer', {
  value: 42,
  writable: true,
  enumerable: true,
  configurable: true
});

The property enumerable specifies a specific property of an object to be available for operations.

The default properties of inbuilt objects in JavaScript are not enumerable that means they can not be listed by using for in loop.

When you try to get the properties of an object, the default properties are not being listed because of enumerable false assigned to them. These properties are not listed when you try to get the properties of that object.

var o = {};
var a = [];
o.prop = 'is enumerable';
a[0] = 'is enumerable';

o.propertyIsEnumerable('prop');   // returns true
a.propertyIsEnumerable(0);        // returns true
a.propertyIsEnumerable('length');   // returns false

One can check if the property is enumerable by using propertyIsEnumerable. The length is a default property of an array object. But it is not listed when you try to get the keys/properties in an array.

How to get success as a software developer

Many times I tried to find some tips on how to get success in the software industry as a developer. I found few of them and thought to share;

  1. One should have Passion in creating something interesting which can make our life easy. With out this the world would not have come to this stage. Every development in the world in somehow related to technology and one should have passion to make something interesting.
  2. One should be dedicated to learn new things which can be implemented in real life.
  3. Sharing is caring. When you share your ideas with other similar people, you might get to accomplish something faster. Moving ahead will be easy with a team.
  4. Be excited to reach to a level and live the life with that in mind even before you have that. Then you will feel the need of it.
  5. Teach things to people, it helps you learn faster. When you teach, you know what are missing in your thinking process. People will fill in those gaps.
  6. Try to finish what is being started.
  7. Stay away from distractions.
  8. Be patient until you receive a response from prospective customers.
  9. Improve your communication skill so that you express your idea to get help.
  10. Keep doing things. Stop Never.

Array.fill method in JavaScript

We are used to create arrays with loop.

A better way to create a long array with around 10,000,000 entries is to use the fill method which is fast and reliable.

E.g.

Var a = new Array(10000000);
a.fill(“satya”);
console.log(a);

Fill can take two extra parameters as well.

a.fill(“satya”, 0, 100);
this will fill the array starting from 0th index till 100th index.

How about an application which suggest where more plantation is needed

Hello World,

Once I got an idea about making an app which can suggest the parts of the city, region, ares which need more plantation. The source for the nearest possible water body to make the plants grow properly.

The data must be shared with one of the Government agency who will analyse the temperature of a particular area and the type of plantation suitable in that area based on water resource availability.

Many times we plant trees which does not produce food items and just for clean air. Once I suggested to plant a fruit tree which will be beneficial for air as well it can produce some food for someone in need. People laughed at me and said if they plant a fruit tree, kids will throw stones and they will suffer from damages.

We eat fruits and throw the seeds. Why not plant them and grow a tree out of it.

Vulnerability with Google Chrome Extension

Hey Google,

Chrome extension is insecure. It allows developers to collect login information from any website without user’s knowledge.

When a chrome extension is installed, It is on for your browser in every session if there is no background script or page.

One can easily inject a piece of JavaScript code to any site and can collect the login details of any user.

A person who understand a bit of JavaScript, can read all the values entered into the input fields. When you read the value of a password field, it will give you the actual password.

From there on click of submit button making an ajax call to your server to store the page URL, username and Password is like cake walk.

Injecting JavaScript to any website from Chrome Extension must be restricted.

Give your thoughts.

Phaser bow and arrow game, trajectory

The most popular phaser framework is very helpful to create html5 and javascript games.

I have come up with a simple trajectory code for a bow and arrow game.


var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', {
  preload: preload,
  create: create,
  update: update
});

var bow, bag, arrow, angle;
var score=0;
var x;
var y;
var oldx, oldy;
var xVel;  // calculate this based on distance
var yVel;  // 
var g = 0.25;
var arrowCreated = false;
var shot = false;

function preload() {
  game.load.image('bag', bagURI);
  game.load.image('bow', bowURI);
  game.load.image('arrow', arrowURI);

}

function create() {
  game.world.setBounds(0, 0, 800, 600);
  bow = game.add.sprite(100, 250, 'bow');
  bow.anchor.setTo(0.5);
  
  bag = game.add.sprite(650,300,'bag');
  bag.anchor.setTo(0.5);
  
  
  arrow = game.add.sprite(bow.x, bow.y, 'arrow');
  arrow.anchor.setTo(0.5);
  arrow.scale.setTo(0.5);
  arrow.angle = bow.angle;
  x = oldx = arrow.x;
  y = oldy = arrow.y;
  //game.input.onDown.add(createArrow, this);
  game.input.onUp.add(shootArrow, this);
}

function update() {
  //console.log(shot);
  if (!shot) {
    angle = Math.atan2(game.input.mousePointer.x - bow.x, -(game.input.mousePointer.y - bow.y)) * (180 / Math.PI) - 180;
    bow.angle = arrow.angle = angle;
    
  } else {
    
    //console.log("x",x,"y",y,"xVel",xVel,"yVel",yVel,"oldx",oldx,"oldy",oldy);
    
    x += xVel;
    y += yVel;
    yVel += g;

    arrow.x = x;
    arrow.y = y;
    
    
    arrowAngle = Math.atan2(x-oldx, -(y-oldy)) * (180 / Math.PI);
    arrow.angle = arrowAngle;
    /* need to write new arrow code */
    
     oldx = x;
     oldy = y;
    
    
    if(arrow.y>600) {
      resetArrow();
    }
    
    
    var intersects = Phaser.Rectangle.intersection(arrow, bag);
    if(intersects.width>0) {
      //console.log(intersects.width);
      resetArrow();
      score=score+45;
      console.log(score);
    }
    
  }
}
function resetArrow() {
  shot = false;
  arrow.x=bow.x;
  arrow.y=bow.y;

  x=oldx=arrow.x;
  y=oldy=arrow.y;
}
function createArrow() {
  arrowCreated = true;
}

function shootArrow() {
  if(!shot) {
    shot = true;
    xVel = - (game.input.mousePointer.x-bow.x)/6;
    yVel = - (game.input.mousePointer.y-bow.y)/6;
  }
}