bagnostian
Author Archives: nasser
geometric
Nasser Torabzade 2015-06-30 14:15:59
Night drive with sib
JavaScript’s Apply, Call, and Bind Methods are Essential for JavaScript Professionals
Prerequisite:
— Understand JavaScript’s “this” With Ease, and Master It.
— JavaScript Objects
— Understand JavaScript Closures
(This is an intermediate to advanced topic)
Duration: About 40 minutes.
Functions are objects in JavaScript, as you should know by now, if you have read any of the prerequisite articles. And as objects, functions have methods, including the powerful Apply, Call, and Bind methods. On the one hand, Apply and Call are nearly identical and are frequently used in JavaScript for borrowing methods and for setting the this value explicitly. We also use Apply for variable-arity functions; you will learn more about this in a bit.
On the other hand, we use Bind for setting the this value in methods and for currying functions.
We will discuss every scenario in which we use these three methods in JavaScript. While Apply and Call come with ECMAScript 3 (available on IE 6, 7, 8, and modern browsers), ECMAScript 5 (available on only modern browsers) added the Bind method. These 3 Function methods are workhorses and sometimes you absolutely need one of them. Let’s begin with the Bind method.
JavaScript’s Bind Method
We use the Bind () method primarily to call a function with the this value set explicitly. It other words, bind () allows us to easily set which specific object will be bound to this when a function or method is invoked.
This might seem relatively trivial, but often the this value in methods and functions must be set explicitly when you need a specific object bound to the function’s this value.
The need for bind usually occurs when we use the this keyword in a method and we call that method from a receiver object; in such cases, sometimes this is not bound to the object that we expect it to be bound to, resulting in errors in our applications. Don’t worry if you don’t fully comprehend the preceding sentence. It will become clear like teardrop in a moment.
Before we look at the code for this section, we should understand the this keyword in JavaScript. If you don’t already understand this in JavaScript, read my article, Understand JavaScript’s “this” With Clarity, and Master It. If you don’t understand this well, you will have trouble understanding some of the concepts discussed below. In fact, many of the concepts regarding setting the “this” value that I discuss in this article I also discussed in the Understand JavaScript’s “this” article.
JavaScript’s Bind Allows Us to Set the this Value on Methods
When the button below is clicked, the text field is populated with a random name.
// <button>Get Random Person</button>
// <input type="text">
var user = {
data :[
{name:"T. Woods", age:37},
{name:"P. Mickelson", age:43}
],
clickHandler:function (event) {
var randomNum = ((Math.random () * 2 | 0) + 1) - 1; // random number between 0 and 1
// This line is adding a random person from the data array to the text field
$ ("input").val (this.data[randomNum].name + " " + this.data[randomNum].age);
}
}
// Assign an eventHandler to the button's click event
$ ("button").click (user.clickHandler);
When you click the button, you get an error because this in the clickHandler () method is bound to the button HTML element, since that is the object that the clickHandler method is executed on.
This particular problem is quite common in JavaScript, and JavaScript frameworks like Backbone.js and libraries like jQuery automatically do the bindings for us, so that this is always bound to the object we expect it to be bound to.
To fix the problem in the preceding example, we can use the bind method thus:
Instead of this line:
$ ("button").click (user.clickHandler);
We simply have to bind the clickHandler method to the user object like this:
$ ("button").click (user.clickHandler.bind (user));
Consider this other way to fix the this value: You can pass an anonymous callback function to the click () method and jQuery will bind this inside the anonymous function to the button object.
Because ECMAScript 5 introduced the Bind method, it (Bind) is unavailable in IE Include this Bind implementation in your code, if you are targeting older browsers:
// Credit to Douglas Crockford for this bind method
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError ("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call (arguments, 1),
fToBind = this,
fNOP = function () {
},
fBound = function () {
return fToBind.apply (this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat (Array.prototype.slice.call (arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP ();
return fBound;
};
}
Let’s continue with the same example we used above. The this value is also bound to another object if we assign the method (where this is defined) to a variable. This demonstrates:
// This data variable is a global variable
var data = [
{name:"Samantha", age:12},
{name:"Alexis", age:14}
]
var user = {
// local data variable
data :[
{name:"T. Woods", age:37},
{name:"P. Mickelson", age:43}
],
showData:function (event) {
var randomNum = ((Math.random () * 2 | 0) + 1) - 1; // random number between 0 and 1
console.log (this.data[randomNum].name + " " + this.data[randomNum].age);
}
}
// Assign the showData method of the user object to a variable
var showDataVar = user.showData;
showDataVar (); // Samantha 12 (from the global data array, not from the local data array)
When we execute the showDataVar () function, the values printed to the console are from the global data array, not the data array in the user object. This happens because showDataVar () is executed as a global function and use of this inside showDataVar () is bound to the global scope, which is the window object in browsers.
Again, we can fix this problem by specifically setting the “this” value with the bind method:
// Bind the showData method to the user object
var showDataVar = user.showData.bind (user);
// Now the we get the value from the user object because the this keyword is bound to the user object
showDataVar (); // P. Mickelson 43
Bind () Allows us to Borrow Methods
In JavaScript, we can pass functions around, return them, borrow them, and the like. And the bind () method makes it super easy to borrow methods.
Here is an example using bind () to borrow a method:
// Here we have a cars object that does not have a method to print its data to the console
var cars = {
data:[
{name:"Honda Accord", age:14},
{name:"Tesla Model S", age:2}
]
}
// We can borrow the showData () method from the user object we defined in the last example.
// Here we bind the user.showData method to the cars object we just created.
cars.showData = user.showData.bind (cars);
cars.showData (); // Honda Accord 14
One problem with this example is that we are adding a new method (showData) on the cars object and we might not want to do that just to borrow a method because the cars object might already have a property or method name showData. We don’t want to overwrite it accidentally. As we will see in our discussion of Apply and Call below, it is best to borrow a method using either the Apply or Call method.
JavaScript’s Bind Allows Us to Curry a Function
Function Currying, also known as partial function application, is the use of a function (that accept one or more arguments) that returns a new function with some of the arguments already set. The function that is returned has access to the stored arguments and variables of the outer function. This sounds way more complex than it actually is, so let’s code.
Let’s use the bind () method for currying. First we have a simple greet () function that accepts 3 parameters:
function greet (gender, age, name) {
// if a male, use Mr., else use Ms.
var salutation = gender === "male" ? "Mr. " : "Ms. ";
if (age > 25) {
return "Hello, " + salutation + name + ".";
}
else {
return "Hey, " + name + ".";
}
}
And we use the bind () method to curry (preset one or more of the parameters) our greet () function. The first argument of the bind () method sets the this value, as we discussed earlier:
// So we are passing null because we are not using the "this" keyword in our greet function.
var greetAnAdultMale = greet.bind (null, "male", 45);
greetAnAdultMale ("John Hartlove"); // "Hello, Mr. John Hartlove."
var greetAYoungster = greet.bind (null, "", 16);
greetAYoungster ("Alex"); // "Hey, Alex."
greetAYoungster ("Emma Waterloo"); // "Hey, Emma Waterloo."
When we use the bind () method for currying, all the parameters of the greet () function, except the last (rightmost) argument, are preset. So it is the rightmost argument that we are changing when we call the new functions that were curried from the greet () function. Again, I discuss currying at length in a separate blog post, and you will see how we can easily create very powerful functions with Currying and Compose, two Functional JavaScript concepts.
So, with the bind () method, we can explicitly set the this value for invoking methods on objects, we can borrow
and copy methods, and assign methods to variable to be executed as functions. And as outlined in the Currying Tip
earlier,
you can use bind for currying.
JavaScript’s Apply and Call Methods
The Apply and Call methods are two of the most often used Function methods in JavaScript, and for good reason: they allow us to borrow functions and set the this value in function invocation. In addition, the apply function in particular allows us to execute a function with an array of parameters, such that each parameter is passed to the function individually when the function executes—great for variadic functions; a variadic function takes varying number of arguments, not a set number of arguments as most functions do.
-
Set the this value with Apply or Call
Just as in the bind () example, we can also set the this value when invoking functions by using the Apply or Call methods. The first parameter in the call and apply methods set the this value to the object that the function is invoked upon.
Here is a very quick, illustrative example for starters before we get into more complex usages of Apply and Call:
// global variable for demonstration var avgScore = "global avgScore"; //global function function avg (arrayOfScores) { // Add all the scores and return the total var sumOfScores = arrayOfScores.reduce (function (prev, cur, index, array) { return prev + cur; }); // The "this" keyword here will be bound to the global object, unless we set the "this" with Call or Apply this.avgScore = sumOfScores / arrayOfScores.length; } var gameController = { scores :[20, 34, 55, 46, 77], avgScore:null } // If we execute the avg function thus, "this" inside the function is bound to the global window object: avg (gameController.scores); // Proof that the avgScore was set on the global window object console.log (window.avgScore); // 46.4 console.log (gameController.avgScore); // null // reset the global avgScore avgScore = "global avgScore"; // To set the "this" value explicitly, so that "this" is bound to the gameController, // We use the call () method: avg.call (gameController, gameController.scores); console.log (window.avgScore); //global avgScore console.log (gameController.avgScore); // 46.4Note that the first argument to call () sets the this value. In the preceding example, it is set to
the gameController object. The other arguments after the first argument are passed as parameters to the
avg () function.The apply and call methods are almost identical when setting the this value except that you pass the function parameters to apply () as an array, while you have to list the parameters individually to pass them to the call () method. More on this follows. Meanwhile, the apply () method also has another feature that the call () method doesn’t have, as we will soon see.
Use Call or Apply To Set this in Callback Functions
I borrowed this little section from my article, Understand JavaScript Callback Functions and Use Them.// Define an object with some properties and a method // We will later pass the method as a callback function to another function var clientData = { id: 094545, fullName: "Not Set", // setUserName is a method on the clientData object setUserName: function (firstName, lastName) { // this refers to the fullName property in this object this.fullName = firstName + " " + lastName; } }function getUserInput (firstName, lastName, callback, callbackObj) { // The use of the Apply method below will set the "this" value to callbackObj callback.apply (callbackObj, [firstName, lastName]); }The Apply method sets the this value to callbackObj. This allows us to execute the callback function with the this value set explicitly, so the parameters passed to the callback function will be set on the clientData object:
// The clientData object will be used by the Apply method to set the "this" value getUserInput ("Barack", "Obama", clientData.setUserName, clientData); // the fullName property on the clientData was correctly set console.log (clientData.fullName); // Barack ObamaThe Apply, Call, and Bind methods are all used to set the this value when invoking a method, and they do it in slightly different ways to allow use direct control and versatility in our JavaScript code. The this value in JavaScript is as important as any other part of the language, and we have the 3 aforementioned methods are the essential tools to setting and using this effectively and properly.
-
Borrowing Functions with Apply and Call (A Must Know)
The most common use for the Apply and Call methods in JavaScript is probably to borrow functions. We can borrow functions with the Apply and Call methods just as we did with the bind method, but in a more versatile manner.
Consider these examples:
- Borrowing Array Methods
Arrays come with a number of useful methods for iterating and modifying arrays, but unfortunately, Objects do not have as many native methods. Nonetheless, since an Object can be expressed in a manner similar to an Array (known as an array-like object), and most important, because all of the Array methods are generic (except toString and toLocaleString), we can borrow Array methods and use them on objects that are array-like.An array-like object is an object that has its keys defined as non-negative integers. It is best to specifically add a length property on the object that has the length of the object, since the a length property does not exist on objects it does on Arrays.
I should note (for clarity, especially for new JavaScript developers) that in the following examples, when we call Array.prototype, we are reaching into the Array object and on its prototype (where all its methods are defined for inheritance). And it is from there—the source—that we are borrowing the Array methods. Hence the use of code like Array.prototype.slice—the slice method that is defined on the Array prototype.
Let’s create an array-like object and borrow some array methods to operate on the our array-like object. Keep in mind the array-like object is a real object, it is not an array at all:
// An array-like object: note the non-negative integers used as keys var anArrayLikeObj = {0:"Martin", 1:78, 2:67, 3:["Letta", "Marieta", "Pauline"], length:4 };Now, if wish to use any of the common Array methods on our object, we can:
// Make a quick copy and save the results in a real array: // First parameter sets the "this" value var newArray = Array.prototype.slice.call (anArrayLikeObj, 0); console.log (newArray); // ["Martin", 78, 67, Array[3]] // Search for "Martin" in the array-like object console.log (Array.prototype.indexOf.call (anArrayLikeObj, "Martin") === -1 ? false : true); // true // Try using an Array method without the call () or apply () console.log (anArrayLikeObj.indexOf ("Martin") === -1 ? false : true); // Error: Object has no method 'indexOf' // Reverse the object: console.log (Array.prototype.reverse.call (anArrayLikeObj)); // {0: Array[3], 1: 67, 2: 78, 3: "Martin", length: 4} // Sweet. We can pop too: console.log (Array.prototype.pop.call (anArrayLikeObj)); console.log (anArrayLikeObj); // {0: Array[3], 1: 67, 2: 78, length: 3} // What about push? console.log (Array.prototype.push.call (anArrayLikeObj, "Jackie")); console.log (anArrayLikeObj); // {0: Array[3], 1: 67, 2: 78, 3: "Jackie", length: 4}We get all the great benefits of an object and we are still able to use Array methods on our object, when we setup our object as an array-like object and borrow the Array methods. All of this is made possible by the virtue of the call or apply method.
The arguments object that is a property of all JavaScript functions is an array-like object, and for this reason, one of the most popular uses of the call () and apply () methods is to extract the parameters passed into a function from the arguments object.
Here is an example I took from the Ember.js source, with comments I added:
function transitionTo (name) { // Because the arguments object is an array-like object // We can use the slice () Array method on it // The number "1" parameter means: return a copy of the array from index 1 to the end. Or simply: skip the first item var args = Array.prototype.slice.call (arguments, 1); // I added this bit so we can see the args value console.log (args); // I commented out this last line because it is beyond this example //doTransition(this, name, this.updateURL, args); } // Because the slice method copied from index 1 to the end, the first item "contact" was not returned transitionTo ("contact", "Today", "20"); // ["Today", "20"]The args variable is a real array. It has a copy of all the parameters passed to the transitionTo function.
From this example, we learn that a quick way to get all the arguments (as an array) passed to a function is to do:
// We do not define the function with any parameters, yet we can get all the arguments passed to it function doSomething () { var args = Array.prototype.slice.call (arguments); console.log (args); } doSomething ("Water", "Salt", "Glue"); // ["Water", "Salt", "Glue"]We will discuss how to use the apply method with the arguments array-like object again for variadic functions. More on this later.
- Borrowing String Methods with Apply and Call
Like the preceding example, we can also use apply () and call () to borrow String methods. Since Strings are immutable, only the non-manipulative arrays work on them, so you cannot use reverse, pop and the like. - Borrow Other Methods and Functions
Since we are borrowing, lets go all in and borrow from our own custom methods and functions, not just from Array and String:var gameController = { scores :[20, 34, 55, 46, 77], avgScore:null, players :[ {name:"Tommy", playerID:987, age:23}, {name:"Pau", playerID:87, age:33} ] } var appController = { scores :[900, 845, 809, 950], avgScore:null, avg :function () { var sumOfScores = this.scores.reduce (function (prev, cur, index, array) { return prev + cur; }); this.avgScore = sumOfScores / this.scores.length; } } // Note that we are using the apply () method, so the 2nd argument has to be an array appController.avg.apply (gameController); console.log (gameController.avgScore); // 46.4 // appController.avgScore is still null; it was not updated, only gameController.avgScore was updated console.log (appController.avgScore); // nullSure, it is just as easy, even recommended, to borrow our own custom methods and functions. The gameController object borrows the appController object’s avg () method. The “this” value defined in the avg () method will be set to the first parameter—the gameController object.
You might be wondering what will happen if the original definition of the method we are borrowing changes. Will the borrowed (copied) method change as well, or is the copied method a full copy that does not refer back to the original method? Let’s answer these questions with a quick, illustrative example:
appController.maxNum = function () { this.avgScore = Math.max.apply (null, this.scores); } appController.maxNum.apply (gameController, gameController.scores); console.log (gameController.avgScore); // 77As expected, if we change the original method, the changes are reflected in the borrowed instances of that method. This is expected for good reason: we never made a full copy of the method, we simply borrowed it (referred directly to its current implementation).
- Borrowing Array Methods
-
Use Apply () to Execute Variable-Arity Functions
To wrap up our discussion on the versatility and usefulness of the Apply, Call, and Bind methods, we will discuss a neat, little feature of the Apply method: execute functions with an array of arguments.
We can pass an array with of arguments to a function and, by virtue of using the apply () method, the function will execute the
items in the array as if we called the function like this:
createAccount (arrayOfItems[0], arrayOfItems[1], arrayOfItems[2], arrayOfItems[3]);
This technique is especially used for creating variable-arity, also known as variadic functions.
These are functions that accept any number of arguments instead of a fixed number of arguments. The arity of a function specifies the number of arguments the function was defined to accept.
The Math.max() method is an example of a common variable-arity function in JavaScript:
// We can pass any number of arguments to the Math.max () method
console.log (Math.max (23, 11, 34, 56)); // 56
But what if we have an array of numbers to pass to Math.max? We cannot do this:
var allNumbers = [23, 11, 34, 56];
// We cannot pass an array of numbers to the the Math.max method like this
console.log (Math.max (allNumbers)); // NaN
This is where the apply () method helps us execute variadic functions. Instead of the above, we have to pass the array of numbers using apply () thus:
var allNumbers = [23, 11, 34, 56];
// Using the apply () method, we can pass the array of numbers:
console.log (Math.max.apply (null, allNumbers)); // 56
As we have learned earlier, the fist argument to apply () sets the “this” value, but “this” is not used in the Math.max () method, so we pass null.
Here is an example of our own variadic function to further illustrate the concept of using the apply () method in
this capacity:
var students = ["Peter Alexander", "Michael Woodruff", "Judy Archer", "Malcolm Khan"];
// No specific parameters defined, because ANY number of parameters are accepted
function welcomeStudents () {
var args = Array.prototype.slice.call (arguments);
var lastItem = args.pop ();
console.log ("Welcome " + args.join (", ") + ", and " + lastItem + ".");
}
welcomeStudents.apply (null, students);
// Welcome Peter Alexander, Michael Woodruff, Judy Archer, and Malcolm Khan.
Final Words
The Call, Apply, and Bind methods are indeed workhorses and should be part of your JavaScript repertoire for setting the this value in functions, for creating and executing variadic functions, and for borrowing methods and functions. As a JavaScript developer, you will likely encounter and use these functions time and again. So be sure you understand them well.
Be Good. Imagine. Create.
Our Career Paths and Courses Website Is Now Live
New UPDATE: June 8, 2015
Enrollment for our Career Paths is well underway. Enroll now. Career Path 1: Junior JavaScript Developer and Career Path 3: Modern Frontend Developer are already full (you may still get a spot if you join the wait list now).
The second cohort for Career Path 5: Modern Fullstack Developer is also full. The second cohort starts immediately after the first cohort of Career Path 3 graduates.
فضیلت انتشار و حفظ کردن حدیث از ائمه اطهار (ع)
فضیلت انتشار و حفظ کردن حدیث از ائمه اطهار (ع)
امام رضا (ع) : خدا رحمت کند کسی را که این امر ولایت ما را زنده
میسازد پرسیده شد امر شما چگونه زنده میشود فرمود علوم و معارف و احادیث ما
را فرا گرفته و به دیگران بیاموزد زیرا مردم اگر با زیبایی های سخنان ما
آشنا گردند از ما پیروی خواهند نمود.
بحارالانوار جلد 2 صفحه 30
رسول اکرم (ص) : اگر کسی دو حدیث را بخواند و خودش به آن دو حدیث عمل نماید و آن را به دیگران نیز تعلیم دهد تا آنان نیز از آن دو حدیث بهره مند شوند چنین کوشش و فعالیتی از جانب او بهتر و بالاتر از ثواب عبادت و بندگی حق تعالی به مدت 60 سال.
منیة المرید صفحه 182
حضرت محمد (ص) : سه بار فرمودند : بار خدایا خلیفه های مرا رحم کن. گفتند یا رسول الله خلیفه های شما چه کسانی هستند ؟ فرمودند آن کسانی که حدیث و سنت مرا تبلیغ کنند و به امت من بیاموزند.
امالی شیخ صدوق مجلس سی و چهارم صفحه 180
امام صادق (ع) : اگر کسی یک حدیث از معارف اسلامی را به خوبی بفهمد و عمق علمی آن را به درستی درک کند ، بهتر از آن است که هزار حدیث را در حافظه خود بسپارد و آنها را نقل نماید ، و پیروان مکتب اهل بیت به مقام فقاهت نائل نمی شوند ، مگر آنکه نظرات ائمه (ع) را درک کنند و به جهات مختلفه احادیث متوجه باشند.
بحارالانوار جلد 1 صفحه 118 ، الحدیث جلد 1 صفحه 366
امام صادق (ع) : احادیث اسلامی را به نوجوانان خود بیاموزید و در انجام این وظیفه تربیتی ، تسریع نمائید ، پیش از آنکه مخالفین گمراه ، بر شما پیشی گیرند و سخنان نادرست خویش را در ضمیر پاک آنان جای دهند و گمراهشان سازند.
الحدیث جلد 1 صفحه 365 ، کافی جلد 6 صفحه 47
امام باقر (ع) : پیروان اسلام در پرتو معرفت واقعی و درک حقایق علمی روایات ، میتوانند به عالیترین مدارج ایمان نائل شوند.
بحارالانوار جلد 1 صفحه 118 ، الحدیث جلد 1 صفحه 365
جابر گوید که امام باقر (ع) فرمود : ای جابر به خدا سوگند ، حدیثی
که از فردی صادق ، در زمینه حلال و حرام به دست آوردی ، از آنچه خورشید بر
آن میتابد با ارزشتر است.
بحارالانوار جلد 2 صفحه 146 حدیث 15
پیامبر گرامی اسلام (ص) فرمود : هرکس از امت من ، چهل حدیث در باب
نیازهای دینی خود حفظ کند ( و عمل کند ) ، خداوند در روز قیامت او را عالمی
فقیه گرداند و از عذاب وی چشم پوشد.
بحارالانوار جلد 2 صفحه 153 حدیث 3 ، خصال شیخ صدوق جلد 2 صفحه 541 حدیث 15 ابواب الاربعین
از امام صادق (ع) سوال شد کدام مرد افضل تر است ؟ مردی که سخنان شما
را نشر میدهد و آن را در دل شیعیان استوار میکند ، یا مردی عابد که چنین
نیست ؟ امام (ع) فرمود : آنکه سخنان ما را نشر دهد و آن را در دل شیعیان
استوار سازد از هزار عابد برتر است.
اصول کافی جلد 1 صفحه 33 حدیث 9
حکمت
98 نهج البلاغه ؛ ضرورت عمل کردن به روایات ؛ (علمى، اجتماعى، تربیتى) و
درود خدا بر او، امام علی (ع) فرمود: چون روایتى را شنیدید، آن را بفهمید
عمل کنید، نه بشنوید و نقل کنید، زیرا راویان علم فراوان، و عمل کنندگان آن
اندکند.
پیامبر صلی الله علیه و آله و سلم فرمود : با یکدیگر ملاقات کنید و به گفتگوی حدیث بپردازید ، زیرا حدیث دلها را صیقل میدهد . دلها زنگار میگیرد ، مانند شمشیر و صیقل آن حدیث است.
اصول کافی جلد 1 صفحه 50 حدیث 8
ابراهیم کَرخی از امام صادق (ع) روایت کرده است که فرمود : یک حدیث را اگر کاملا فهمیده باشی ، ارزشمندتر از هزار روایت است که ( طوطی وار ) آنرا برای دیگران نقل کنی ، و نیز هیچ یک از شما در قوانین و احکام دین عالم نخواهند بود مگر آنانکه مفهموم های گوناگون از سخن ما را دریافته باشد ؛ یقینا هر جمله ای از گفتار ما به هفتاد گونه تعبیر میگردد که راه خروج از هریک از آنها برای ما باز است.
معانی الاخبار جلد 1 صفحه 5
(در باره كسى كه چهل حدیث نگهدارى كند)
رسول خدا (ص) فرمود: از افراد امت من هر كس كه چهل حدیث از آنچه مردم در
كار دینشان نیازمند به آن میباشند نگهدارى كند خداوند روز قیامت او را
فقیهى دانشمند برانگیزد.
پیغمبر (ص) فرمود : هر كه از امت من چهل حدیث از سنت نگهدارى كند بروز قیامت من از اوشفاعت خواهم كرد.
رسول خدا (ص) فرمود: هر كس از امت من در باره دین چهل حدیث از من
نگهدارى كند و مقصودش رضاى خدا و ثواب عالم آخرت باشد خداوند او را بروز
قیامت فقیهى دانشمند مبعوث خواهد فرمود.
حنانة بن سدیر گوید: شنیدم امام صادق علیه السّلام میفرمود: كسى كه چهل
حدیث از ما از حدیثهائى كه ما در باره حلال و حرام گفته ایم نگهدارى كند
خداوند روز قیامت او را فقیهى دانشمند بر انگیزد و او را عذاب نفرماید.
حسین بن على علیه السّلام؟ فرمود: رسول خدا (ص) امیر المؤمنین على بن
ابى طالب (ع) را وصیت فرمود و از جمله وصیتهایش این بود كه به او فرمود: یا
على، كسى كه از امت من چهل حدیث نگهدارى كند و در این كار رضاى خداوندى و
پاداش عالم آخرت را خواسته باشد خداوند بروز قیامت او را با پیغمبران و
راستان
و شهیدان و بندگان شایسته محشور فرماید و اینان رفیقان خوبى
هستند على (علیه السّلام) عرض كرد : یا رسول اللَّه مرا خبر بده كه این
حدیثها چه باشند؟ فرمود: اینكه به خداى یگانه بدن شرک ایمان بیاورى و او
را بپرستى و جز او را نپرستى و نماز را با وضوى كامل در وقتهاى خودش
بخوانى و تأخیرش میاندازى كه بدون جهت نماز را تأخیر انداختن باعث خشم خداى
عزوجل است ، زكاة را بپردازى و ماه رمضان را روزه بگیرى و اگر ثروتى دارى
و توانائى رفتن به مكه را دارى حج خانه خدا را انجام دهى و پدر و مادر خود
را ناراضى نكنى و مال یتیم را از راه ستم نخورى و ربا نخورى و مِى َننُوشى
و نه هیچ از نوشابه هائى كه مستى آورد، و زنا نكنى و لواط نكنى و در راه
سخن چینى قدم بر ندارى و بخدا سوگند دروغ یاد نكنى و دزدى نكنى و گواهى
دروغین به هیچ كس از خویش و بیگانه ندهى و حق را هر كس كه براى تو آورد
بپذیرى آورنده كوچك باشد یا بزرگ و به هیچ ستمگرى گرچه دوست نزدیكت باشد
اعتماد نكنى و هوا پرست نباشى و زن پاك دامن را متهم نسازى و ریا نورزى كه
كمترین ریا شرك به خداى عزوجل است و به منظور عیبجوئى بشخص كوتاه نگوئى ،
اى كوتاه و بشخص بلند قامت نگوئى ، اى دراز و هیچ یك از خلق خدا را مسخره
نكنى و در گرفتارى و مصیبت شكیبا باشى و به نعمتهائى كه خداوند به تو عطا
فرموده سپاسگزار باشى و بواسطه گناهى كه آلوده شده اى از عذاب خدا ایمن
نباشى و از رحمت خداوند مأیوس نباشى و از گناهت بسوى خداى عزوجل توبه كنى
كه هركس از گناهانش توبه كار گردد مانند كسى است كه گناهى نكرده است و با
اینكه از خداوند آمرزش می طلبى اصرار بر گناه نورزى كه مانند كسى خواهى بود
كه بخدا و آیات خدا و پیغمبران خدا مسخره بكند و بدانى كه آنچه كه بر سرت
آید خطا پذیر نبوده و آنچه بتو نرسید رسیدنى نبوده است و از پى كارى كه
رضاى مخلوق در آن باشد ولى خشم خداى را برانگیزد نروى و دنیا را بر آخرت
مقدم ندارى كه دنیا گذران است و آخرت پاینده و از آنچه میتوانى نسبت به
برادرت بخل نورزى و باطنت مثل ظاهرت باشد نه آنكه ظاهرت زیبا و باطنت زشت
باشد كه اگر خوش ظاهر و بد باطن باشى از منافقان خواهى بود و دروغ نگوئى و
با دروغگویان میامیزى و چون حق بشنوى خشمناك نشوى و تا آنجا كه میتوانى خود
و خانواده و فرزندان و همسایگان را ادب كنى و آنچه را كه میدانى بكار بندى
و با هیچ یك از خلق خداى عزوجل جز به حق رفتار نكنى و نسبت بخویش و بیگانه
سخت گیر نباشى و ستمگر و كینه توز نباشى و بسیار بگوئى { سبحان اللَّه و
لا اله الا اللَّه } و دعا فراوان كنى و به یاد مرگ و بعد از مرگ از
قیامت و بهشت و دوزخ بسیار باشى و قرآن زیاد بخوانى و بآنچه در آن است عمل
كنى و نیكوكارى و احترام نسبت به مرد و زن مؤمن را غنیمت به شمارى و خوب
دقت كنى هر چه را كه براى خودت نمی پسندى در باره هیچ یك از مؤمنین انجام
ندهى و از انجام كار نیك دلتنگ نباشى و بار دوش هیچ كس نگردى و چون به كسى
نعمتى دادى منتش نَنَهى و دنیا در نزد تو همچون زندان باشد تا خداوند بهشتى
را نصیبت فرماید این چهل حدیثى است كه هر كس از امت من بر آنها پایدار
باشد و آنها را نگهدارى كند به حكم رحمت الهى به بهشت داخل میشود و از
بهترین مردم است و پس از پیغمبران و اوصیاء پیغمبران محبوبترین افراد نزد
خدا است و خداوند او را بروز قیامت با پیغمبران و صدیقان و شهیدان و
شایستگان محشور خواهد فرمود و اینان رفیقان خوبى هستند.
خصال شیخ صدوق جلد 2 صفحه 643 الی 646
Antonio Mora. Where
Nasser Torabzade 2015-06-28 20:45:29
#photography
Sergio Larrain
A day in the land of
Black Forest, German
Nasser Torabzade 2015-06-28 20:30:30
nasser-torabzade starred caolan/async
ایات و روایات درباره صلوات
آیات و روایات درباره صلوات
إِنَّ اللَّهَ وَ مَلَئكَتَهُ یُصَلُّونَ عَلىَ النَّبىِِّ
یَأَیهَُّا الَّذِینَ ءَامَنُواْ صَلُّواْ عَلَیْهِ وَ سَلِّمُواْ
تَسْلِیمًا ( سوره احزاب آیه 56 )
( خدا و ملائكه خدا درود
میفرستند بر پیغمبر ، اى كسانى كه ایمان آورده اید ! صلوات بفرستید بر
پیغمبر و سلام گویید او را سلام نیكو )
امام ششم (ع) : چون خواهى دعا كنى خداوند عزوجل را تمجید كن، و حمد گو و تسبیح نما، و تهلیل بگو و او را ثنا نما و بر نبى اكرم (ص) درود فرست و سپس دعا كن كه خواسته ات برآورده شود.
مکارم اخلاق جلد 2 صفحه 13
امام صادق (ع) فرمودند : مردى به مسجد در آمد، و دو ركعت نماز خواند و از خداوند حاجت خواست ، پیغمبر (ص) فرمود: این بنده عجله كرد ، دیگرى آمد و نماز گزارد و خدا را مدح گفت و صلوات فرستاد، پیامبر(ص) فرمود: حاجتت را بخواه كه برآورده خواهد شد.
مکارم اخلاق جلد 2 صفحه 14
امام صادق (ع) : همواره دعا از خدا محجوب خواهد بود تا آنگاه كه دعا كننده صلوات بفرستد.
مکارم اخلاق جلد 2 صفحه 15
حضرت صادق (ع): هر كه دعا كند و درود به پیغمبر (ص) نفرستد دعا بر سرش بال زنان می ایستد (یعنى بالا نمیرود) و چون بر پیغمبر (ص) صلوات فرستد دعایش بالا رود.
مکارم اخلاق جلد 2 صفحه 16
امام صادق (ع) : مردى محضر نبىّ اكرم (ص) شرفیاب شد و عرض كرد، یا رسول الله ثلث نه بلكه نصف، بلكه همه درودهایم را نثار شما می كنم حضرت فرمود: با این كار فیض دنیا و آخرتت را تأمین كرده اى ، ابى بصیر و ابن الحكم گویند: از حضرت صادق (ع) پرسیدم مراد از اینكه ثلث صلوات و درود را نثار تو میكنم یعنى چه ؟ فرمود : درود را قبل از هر دعایى می فرستد، و از خداوند هیچ چیز نمی طلبد مگر آنكه سخن خود را به نام پیغمبر آغاز می كند، و بعد حاجت می طلبد.
مکارم اخلاق جلد 2 صفحه 16
حضرت صادق (ع): هر كه را حاجتى باشد اوّل صلوات بفرستد ، بعد از خدا حاجت بخواهد، و دعا را نیز به صلوات ختم كند، كه خداوند متعال كریمتر از آنست كه دو طرف دعا را قبول كند و وسط آن را فراموش نماید كه صلوات هرگز بی استجابت نخواهد ماند.
مکارم اخلاق جلد 2 صفحه 17
از امام ششم : پیغمبر (ص) فرمود : هیچ قومى در مجلسى جمع نشوند كه در آن مجلس یاد خداوند و صلوات بر محمّد (ص) نباشد ، مگر آنكه آن مجلس حسرت و وبال بر مجلسیان خواهد بود.
مکارم اخلاق جلد 2 صفحه 17
از نبى اكرم روایت است كه : چون اسم پیغمبر(ص) برده شود بسیار بر وى صلوات بفرستید ، كه هر كه یك صلوات بر پیغمبر (ص) بفرستد، هزار صف فرشته بر او درود می فرستند، هیچ مخلوقى نماند مگر آنكه بروى درود میفرستند، زیرا كه خدا و فرشتگان بروى صلوات میفرستند، و هر كه به این ثواب عظیم میل نشان ندهد جاهل و مغرور است ، و خداوند و پیغمبر (ص) از وی بری و بیزار هستند.
مکارم اخلاق جلد 2 صفحه 100
از نبى اكرم (ص) : من در قیامت نزد میزان هستم ، و هر كه گناهانش بر ثوابهایش فزونى گیرد صلواتهائى را كه بر من فرستاده می آورم تا بدانها میزان اعمالش سنگین گردد.
مکارم اخلاق جلد 2 صفحه 101
از على (ع): هیچ دعائى به آسمان بالا نمیرود، مگر آنكه بر محمد و آل او درود فرستاده شود.
مکارم اخلاق جلد 2 صفحه 101
از امام صادق (ع) : در یكى از كتب خوانده ام كه هر كس بر محمد (ص) صلوات فرستد خداوند صد حسنه برایش بنویسد، و هر كه بگوید: صلوات و درود خدا بر محمد و اهل بیت او خداوند هزار حسنه برایش بنویسد.
مکارم اخلاق جلد 2 صفحه 101
نبى اكرم (ص) : سزاوارترین مردم به من در قیامت آن كس است كه بیشتر بر من درود فرستد.
مکارم اخلاق جلد 2 صفحه 101
حضرت محمد (ص) : بخیل كسى است كه نام من نزد او برده شود و او بر من صلوات نفرستد.
مکارم اخلاق جلد 2 صفحه 101
حضرت محمد (ص) : هر فردى از امّتم كه با اخلاص بر من صلوات فرستد خدا ده بار بر او درود فرستد و ده درجه او را بالا برد و ده ثواب برایش بنویسد و ده گناه از نامه عملش محو گرداند.
مکارم اخلاق جلد 2 صفحه 102
حضرت محمد (ص) فرمودند : بلند بر من صلوات بفرستید كه این عمل نفاق را ببرد.
مکارم اخلاق جلد 2 صفحه 102
یزید بن حسن گوید: حضرت موسى بن جعفر علیهما السلام برایم چنین گفت: كه (پدرم امام صادق علیه السلام فرمود:) شخصى كه بر پیامبر صلوات می فرستد مفهومش آن است كه من بر سر پیمان خود باقى هستم، و به «بلى» كه در عالم «ذرّ» به سؤال أَ لَسْتُ بِرَبِّكُمْ (آیا من پروردگار شما نیستم- سوره اعراف آیه 172) گفته ام وفادارم.
معانی الاخبار جلد 1 صفحه 269
ناجیه از حضرت باقر علیه السّلام روایت كند كه فرمود : در روز جمعه هر گاه نماز عصر را خواندى ، پس بگو:
«اللّهمّ
صلّ على محمّد و آل محمّد الأوصیاء المرضیین بأفضل صلواتك، و بارك علیهم
بأفضل بركاتك، و السّلام علیهم و على أرواحهم و أجسادهم و رحمة اللَّه و
بركاته»
كه ترجمه آن اینست: بار الها درود بفرست بر محمّد و آل محمّد جانشینانش كه از آنها راضى و خشنود میباشى به درجه اعلاى رحمتت و بركت بر آنان نازل كن به بالاترین بركت هایت، درود بر آنها باد و بر روحشان و بدنشان و رحمت خدا و بركاتش نیز.پس هر كسى كه این صلوات را بعد از نماز عصر بگوید، خداوند صد هزار حسنه براى او بنویسد، و صد هزار گناه كوچك از او محو كند، و صد هزار حاجت او برآورد، و صد هزار درجه از براى او بالا برد.
ثواب الاعمال عقاب الاعمال صفحه 88
عاصم بن ضَمرَه گوید: امیرالمؤمنین علیه السّلام فرمود: فرستادن صلوات بر پیغمبر خدا صلّى اللَّه علیه و اله و سلّم بهتر از آبى كه آتش را خاموش كند ، گناهان را از بین میبرد ، و سلام بر آن حضرت أفضل است از آزاد كردن چند بنده از قید بردگى، و دوستى رسول خدا صلّى اللَّه علیه و اله و سلّم افضل و برتر از جان نثارى و ریختن خونها - یا فرمود : به كار بردن شمشیرها- در راه خدا است.
ثواب الاعمال عقاب الاعمال صفحه 342
از ابى بصیر روایت شده كه امام صادق علیه السّلام فرمود: هنگامى كه نام
پیغمبر صلّى اللَّه علیه و اله و سلّم برده می شود بر آن حضرت بسیار صلوات
بفرستید زیرا هر كس یك بار بر او صلوات فرستد خداوند هزار بار در هزار صف
از فرشتگان بر وى صلوات فرستد، و هیچ مخلوقى باقى نماند مگر اینكه بر آن
بنده صلوات فرستد از جهت صلوات خداوند و فرشتگانش بر او، و دست نكشد از این
ثوابها و بی رغبتى ننماید مگر نادان خود بین كه خدا و رسول از وى
بیزارند.
شرح: مراد از صلوات خداوند و فرشتگان فرو ریختن رحمت و بركت و لطف و كرامت و نعمت خدا است بر بنده خویش.
ثواب الاعمال عقاب الاعمال صفحه 342
أبوالبخترى (كه سنّى مسلك است) از امام صادق از پدرانش علیهم السّلام از رسول خدا صلّى اللَّه علیه و اله و سلّم روایت كرده كه فرمود: من در قیامت نزد میزان اعمال باشم هر كس كفّه گناهانش افزونتر از حسناتش شود آن صلواتهائى را كه بر من فرستاده بیاورم و بر حسناتش بیفزایم تا بر گناهانش فزون آید.
ثواب الاعمال عقاب الاعمال صفحه 344
عبد السّلام (بن عبد الرّحمن) بن نعیم گوید: به امام صادق علیه السّلام عرض كردم من داخل كعبه شدم و دعائى به خاطرم نیامد جز صلوات بر محمّد صلّى اللَّه علیه و اله و سلّم فرمود: آگاه باش مانند تو در فضیلت و ثواب كسى از خانه خدا بیرون نیامده است.
ثواب الاعمال عقاب الاعمال صفحه 345
حارث همدانى گوید: امیرالمؤمنین علیه السّلام فرمود: هیچ دعائى به آسمان راه ندارد تا اینكه بر محمّد و اهل بیتش علیهم السّلام صلوات فرستاده شود.
ثواب الاعمال عقاب الاعمال صفحه 345











