javascript - Using JS OOP with PHP
I'm new at JS programming. I have to write a php webapplication which uses ajax calls for every action (can't reload the page). So I have the php code done, and the JS as well, but I don't know what does JS OOP mean. I did google search for hours, and all I could figure out was "JS has no classm but there are tricks". So I would like to ask you to lead me to the right way and show me how to make it right.
I have four js files:
content.js, function.js, onload.js and validate.js
For now, I only do care about the function.js. It has code only for the ajax calls. Their names are:
NewMember(), SearchMembers(), ShowDetails(), DeleteMembers() and EditMember()
I'm going to show you only the first function (I don't want to waste your time by duplicating since each function is totally similar)
function NewMember() {
if (checkFields() == true) {
Loading("start");
var dataArray = {
first_name : $('#first_name').val(),
last_name : $('#last_name').val(),
birth_date : $('#birth_date').val(),
home_phone : $('#home_phone').val(),
work_phone : $('#work_phone').val(),
email : $('#email').val(),
email_again : $('#email_again').val(),
country : $('#country').val(),
city : $('#city').val(),
address : $('#address').val(),
comment : $('#comment').val()
};
var JSONString = JSON.stringify(dataArray);
$.ajax({
type: 'POST',
url : 'adapter.php',
data: {
data : JSONString,
action : 'create' },
success: function(msg) {
alert(msg);
resetFieldValues();
SearchMembers();
},
error: function() {
alert("Oops! Something went wrong, please reload the page!");
}
});
}
}
My question is, how would you re-write this by using the JS OOP? Full code is not neccessary, but please give me "easy" answers since I'm really a newbie. I'm really confused :/ Thanks for your help!
Answer
Solution:
JavaScript is a completely object-oriented language.
Instead of the Classical Inheritance model that programmers are usually familiar with, JavaScript has a unique take on things with its model of Prototypical Inheritance.
Consider classical inheritance in PHP:
This is arguably the simplest methodology in JavaScript for the same faculty:
In JavaScript, you create Prototype Chains. When you ask an object for a property, JavaScript will first check the object itself for that property -- if it isn't found, JavaScript will check the object's prototype object. If the property isn't there either, JavaScript will check that prototype object's own prototype -- and so on, and so forth, bubbling up the Prototype Chain until the property is found (or not).
Answer
Solution:
JS means JavaScript. OOP means Object Oriented Programming. This is programming JavaScript in an OOP manner. It doesn't have a formal definition of classes in the programming language per say, it has functions, variables and objects (yes, objects, even if it does not have classes). Basically your JavaScript acts as part of your controller and should call PHP functions to carry out requests on the server. Remember OOP usually takes the MVC (Model View Controller) methodology.
So this code should fire when a message is received from an action. Pass the information to the PHP on the server and the PHP should save the information (as a JSON file or in a SQL database for example).
Answer
Solution:
It's important to note that "object-oriented programming" doesn't necessarily mean "class-based programming", so looking online for "how to write a class in JavaScript" will lead you up a blind alley. (JavaScript instead implements prototype-based programming which should be viewed as a different way to do OOP.)
In a lot of the simple cases you'll have in client-side scripts, you don't need to worry about inheritance, you just want to expose an object which has public methods and private state. So instead of diving right into prototypal inheritance (which takes a bit of getting used to), start by learning how to implement encapsulation, and privately scoped variables.
Here's a succinct introduction to basic public and private members, and below (and in this demo) is a quick abstract example of encapsulation (but no inheritance) using JavaScript's object model:
Note: I'm not an expert on JS OOP, and there may be gotchas or bad practices in my example above; if anyone more experienced spots one, please let me know in a comment.