Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Static members in JavaScript
Play lesson

JavaScript Tutorial - Static members in JavaScript

4.0 (0)
13 learners

What you'll learn

This course includes

  • 12.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

Link for all dot net and sql server video tutorial playlists http://www.youtube.com/user/kudvenkat/playlists Link for slides, code samples and text version of the video http://csharp-video-tutorials.blogspot.com/2015/02/static-members-in-javascript.html Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 In an object oriented programming language, classes can have static members, that is static methods and static fields. For example a class in C# is shown below. In this example PI is a static field and _radius is an instance field. public class Circle { // Static field static float PI = 3.141F; // Instance field public int Radius; public Circle(int raidus) { this.Radius = raidus; } public float CalculateArea() { return Circle.PI * this.Radius * this.Radius; } } What is the difference between static member and instance member and when to use one over the other An instance member belong to a specific instance. So if I create 3 instances (objects) there will be 3 copies of instance fields in memory where as there will ever be only one copy of a static field no matter how many instances we create. In the above example, radius is specific to the circle instance you are creating but the PI value need to be shared by all the circle objects. So, if you want a member to be shared by all instances then make it a static member otherwise make it an instance member. Since JavaScript is also an object oriented programming language, objects in JavaScript can also have static and instance fields and methods. Here is an example. In this example PI is a static field and _radius is an instance field. function Circle(radius) { // Instance field this.Radius = radius; // Static field Circle.PI = 3.141; this.CalculateArea = function () { return Circle.PI * this.Radius * this.Radius; } } var circleObject = new Circle(10); document.write("Area = " + circleObject.CalculateArea()); Points to remember 1. Define a static member using the name of the constructor function. 2. To invoke a static member use the name of the constructor function. 3. To invoke an instance member use the instance of the constructor function. Static methods in JavaScript : In the following example ShowCount() is a static method. Notice that, just like a static variable, static method is also defined using the name of the constructor function. To keep track of the number of Shape objects created we are using Shape.Count static field. Shape.ShowCount() method returns the count of shapes when called. function Shape(shapeName) { // Instance field this.ShapeName = shapeName; // Static Field Shape.Count = ++Shape.Count || 1; // Static method Shape.ShowCount = function () { return Shape.Count; } } var shape1 = new Shape("Circle"); var shape2 = new Shape("Rectangle"); var shape3 = new Shape("Triangle"); document.write("Shape.Count = " + Shape.ShowCount()); Since we have created 3 shapes, the output will be 3.

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere