http://www.youtube.com/user/kudvenkat...
Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspo...
In any object oriented programming language, classes can have private and public members. For example a class in C# can have private and public fields and functions. An example is shown below.
public class Employee
{ // Private Field private string privateFullName; // Public Fields public string firstName; public string lastName; // Private Function private string privateGetFullName() { privateFullName = this.firstName + " " + this.lastName; return privateFullName; } // Public Function public string publicGetFullName() { return privateGetFullName(); }
}
JavaScript is object oriented programming language, so objects in JavaScript can also have private and public fields and functions. An example is shown below.
function Employee(firstName, lastName)
{ // Private Field var privateFullName; // Public Fields this.firstName = firstName; this.lastname = lastName; // Private Function var privateGetFullName = function () { privateFullName = firstName + " " + lastName; return privateFullName; } // Privileged Function this.privilegedGetFullName = function () { return privateGetFullName(); } // Public Function Employee.prototype.publicGetFullName = function () { return this.privilegedGetFullName(); }
}
In the example above, we also have a privileged Function. So, what is a Privileged Function?
1. Privileged methods are created using "this" keyword and Public methods are created using prototype property of the constructor function.
2. Privileged method can access private variables and methods
3. Public methods can call Privileged methods but not Private methods.
4. Like Public methods, Privileged methods are also available outside the constructor function.
Public fields and functions are available both inside and outside of the Employee() constructor function. Private fields and functions are available only inside the Employee() constructor function. Attempting to access private fields and properties outside of the constructor function will result in undefined error.
var employee = new Employee("Tom", "Grover");
document.write(employee.publicGetFullName() + "[br/]"); // Calling public function works fine
document.write(employee.privilegedGetFullName() + "[br/]"); // Calling Privileged function works fine
employee.privateGetFullName() // Calling private method - undefined error
employee.privateFullName // Calling private field - undefined error
Can we modify a private field outside of the constructor function?
Straight answer, no you can't.
In the example below, when we call the private field (employee.privateFullName) it results in undefined error. On the next line we are adding a new public field with same name as the private field to the employee object. Is this going to change the private field (privateFullName). The answer is NO. You cannot access or modify private fields outside of the object. In this example, you are just adding new public field (employee.privateFullName) to the employee object.
var employee = new Employee("Tom", "Grover");
employee.privateFullName // Calling private field - undefined error
// Add a field with same name as private field
employee.privateFullName = "ABC";
document.write(employee.publicGetFullName() + "[br/]");
document.write(employee.privateFullName);
Here is the summary
Private fields - Declared using the var keyword inside the object, and can only be accessed by private functions and privileged methods.
Public fields - Declared using this keyword and are available outside the object.
Private functions - Declared inside the object using one of the 2 ways shown below. Can be called only by privileged methods.
var privateGetFullName = function ()
{ privateFullName = firstName + " " + lastName; return privateFullName;
}
OR
function privateGetFullName()
{ privateFullName = firstName + " " + lastName; return privateFullName;
}
Privileged methods - Declared using this keyoword and are available both within and outside the object.
// Privileged Function
this.privilegedGetFullName = function ()
{ return privateGetFullName();
}
Public methods - Defined by using the object's prototype property and are available both within and outside the object.
// Public Function
Employee.prototype.publicGetFullName = function ()
{ return this.privilegedGetFullName();
}
asp.net core docker Private members in JavaScript | |
185 Likes | 185 Dislikes |
42,879 views views | 524K followers |
Education | Upload TimePublished on 4 Feb 2015 |
Related keywords
wcf vs web api,ado.net core,sql server management studio,webkinz,webadvisor,craigslist nj,wcf one piece,asp.net core 3,ajax players,weber grill parts,webtoon,tutorials by hugo,csharp corner,mvc design pattern,asp.net machine account,servers for minecraft,asp.net core dependency injection,ado.net tutorial,services angular,ajax ontario,asp.net mvc tutorial,javascript tutorial date,asp.net cos'è,csharp assembly,javascript tutorialspoint,tutorialspoint python,sql join,services briefcase,asp.net core web api,ajax jquery,wccftech,craigslist ny,asp.net zero,csharp foreach,server memes,sql date format,javascript tutorial example,services online,chase,javascript tutorial callback,serverless architecture,server resume,wcf c#,server books,tutorialspoint javascript,mvcc connect,ado.net mysql,services technologies gps,javascript tutorial indonesia pdf,ajax deadpool,server jobs,cvs,website,mvc tutorial,costco hours,wcf service application,tutorialspoint spring,serverless,wcf soap,javascript tutorial code,wcf cat,wcf test client,cool math games,services & training hse colombia sas,servicestack,citibank,asp.net core identity,sql union,ajax parking,sql database,asp.net core logging,mvconnect,asp.net guida,cunyfirst,wcf nba,csharp download,wcfi foundation,csharp online,wcf authentication,javascript tutorial pdf,tutorials near me,http://asp.net,server 2019,ado.net descargar,web of dreams,chernobyl,serverminer,ajax cleaner,ado.net visual studio 2019,webassign,ado.net vs entity framework,ado.net visual studio 2017,csharp list,sql like,asp.net mvc,asp.net core tutorial,sqlite,wcfm,ajax roster,mvc architecture,http://ado.net,asp.net core mvc,ajax soccer,server hosting,wcfi,ajax dish soap,capital one,server rack,tutorialspoint html,csharp interface,craigslist,webroot,tutorialspoint reactjs,ajax request,wcf dragon ball,asp.net core 2.2,tutorialspoint python 3,sql developer,webster,services transmission company sas,sql group by,asp.net core signalr,services manager,mvc framework,ajax paving,mvc near me,tutorialspoint spring boot,mvc map,csharp online compiler,asp.net download,sql between,ado.net c# pdf,wcf 2019 nba,javascript tutorial download,services tag dell,csharp switch,ado.net ventajas y desventajas,csharpstar,wcf tutorial,javascript tutorial web development,tutorialspoint,ajax meaning,csharp-video-tutorials.blogspot,tutorials dojo,csharp string format,central park 5,ado.net c#,javascript tutorial for beginners,asp.net core github,server status,ajax fc,server jobs nyc,asp.net core swagger,sql formatter,credit karma,services group,server error in '/' application,services windows,asp.net core 3.0,sql injection,wcf ria services,tutorialspoint c#,calculator,ado.net entity data model,sql insert,tutorialspoint tableau,services google play apk,sqlyog,asp.net core 3 release date,sql server,server job description,tutorials by a,servicenow,webcam,mvc hours,javascript tutorial library,webmd symptom,csharp array,javascript tutorial w3,csharp enum,ajax call,asp.net core 2. guida completa per lo sviluppatore,asp.net core,server pro,server status ffxiv,webcrims,cheap flights,asp.net core hosting,services sas,javascript tutorial documentation,tutorialspoint java,tutorialspoint java compiler,webmd,csharp to json,college football,ado.net dataset,csharp dictionary,cnn,website builder,tutorialspoint sql,asp.net web api,server side rendering,weber grills,sql server 2017,mvc nj,tutorialspoint spark,javascript tutorial function,ado.net oracle,asp.net core download,csharp to vb.net,webster bank,webstaurant,tutorialsystems,ajax post,services fms publish announcement,services.msc no abre,ajax jersey,csharp operator,asp.net core razor pages,server duties,asp.net core environment variables,csharp random,century 21,services consultores,services consulting,mvcsd,mvcsc,services.msc,ado.net pdf,asp.net core configuration,ajax marvel,sql update,asp.net tutorial,mvc medical,ado.net entity data model visual studio 2019,wcfs international curriculum,mvc2,javascript tutorial w3school,ado.net entity data model visual studio 2017,javascript tutorial github,chase online,wcf api,costco,server jobs near me,webex,sql meaning,sql tutorial,sql commands,javascript tutorial video,ado.net entity framework,ajax tavern,tutorialsteacher,ajax javascript,javascript tutorial for frontend developers,services desk,ajax greek,csharp tutorial,mvc pattern,ado.net sql server,ado.net connection,asp.net identity,mvcu,asp.net core middleware,wcf web service,mvc webadvisor,web store,mvcc,webmail,mvci,mvctc,
Không có nhận xét nào:
Đăng nhận xét