var handleEvent = function (color) { returnfunction (e) { e.target.style.color = color console.log(this) } }
for (var i = 0; i < nodeList.length; i++) { nodeList[i].addEventListener('click', handleEvent(colorList[i])) }
四、ES6
1 2 3 4 5 6 7
const len = nodeList.length for (let i = 0; i < len; i++) { nodeList[i].addEventListener('click', e => { console.log(this) e.target.style.color = colorList[i] }) }
for (var i = 0; i < nodeList.length; i++) { map.set(nodeList[i], newLight(nodeList[i], colorList[i])) } document.querySelector('ul').addEventListener('click', e => map.get(e.target).switch())
Function.prototype.bind = Function.prototype.bind || function (ctx, ...formerArgs) { if (typeofthis !== 'function') { thrownewTypeError("NOT_A_FUNCTION -- this is not callable"); } let that = this; let fNOP = function () {}; let args = Array.prototype.slice.call(arguments, 1);
let fbound = function (...laterArgs) { that.apply(thisinstanceof fNOP ? this : ctx, args.concat(laterArgs)); };
const len = nodeList.length for (let i = 0; i < len; i++) { nodeList[i].addEventListener('click', e => { console.log(this) e.target.style.color = colorList[i] }) }
ES6 for of array.entries()
1 2 3 4 5 6
consthandleEvent = color => e => e.target.style.color = color const iterator = nodeList.entries();
for (let [i, node] of iterator) { node.addEventListener('click', handleEvent(colorList[i])) }
1 2 3 4 5 6
Function.prototype.bind = function (context) { var that = this; returnfunction () { that.apply(context); } }
1 2 3 4 5 6
Function.prototype.bind = Function.prototype.bind || function(context) { var that = this; returnfunction() { return that.apply(context, arguments); } }
1 2 3 4 5 6 7 8 9
Function.prototype.bind = function (context) { var that = this; var args = Array.prototype.slice.call(arguments, 1);
Function.prototype.bind = function (context) { var that = this; var args = Array.prototype.slice.call(arguments, 1);
var fbound = function () { var bindArgs = Array.prototype.slice.call(arguments); that.apply(thisinstanceof that ? this : context, args.concat(bindArgs)); } fbound.prototype = this.prototype; return fbound; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Function.prototype.bind = Function.prototype.bind || function (context, ...formerArgs) { let that = this;
if (typeofthis !== 'function') { thrownewTypeError("NOT_A_FUNCTION -- this is not callable"); } let fNOP = function () {};
let fbound = function (...laterArgs) { that.apply(thisinstanceof that ? this : context, formerArgs.concat(laterArgs)); };
Our neighbour, Captain Charles Alison, will sail from Portsmouth tomorrow. We’ll meet him at the harbour early in the morning. He will be in his small boat, Topsail. Topsail is a famous little boat. It has sailed across the Atlantic many times. Captain Alison will set out at eight o’clock, so we’ll have plenty of time. We’ll see his boat and then we’ll say goodbye to him. He will be away for two months. We are very proud of him. He will take part in an important race across the Atlantic.
I was having dinner at a restaurant when Tony Steele came in. Tony worked in a lawyer’s office years ago, but he is now working at a bank. He gets a good salary, but he always borrows money from his friends and never pays it back. Tony saw me and came and sat at the same table. He has never borrowed money from me. While he was eating, I asked him to lend me twenty pounds. To my surprise, he gave me the money immediately. ‘I have never borrowed any money from you,’ Tony said, ‘so now you can pay for my dinner!’
We have an old musical instrument. It is called a clavichord. It was made in Germany in 1681. Our clavichord is kept in the living room. It has belonged to our family for a long time. The instrument was bought by my grandfather many years ago. Recently it was damaged by a visitor. She tried to play jazz on it! She struck the keys too hard and two of the strings were broken. My father was shocked. Now we are not allowed to touch it. It is being repaired by a friend of my father’s.
On Wednesday evening, we went to the Town Hall. It was the last day of the year and a large crowd of people had gathered under the Town Hall clock. It would strike twelve in twenty minutes’ time. Fifteen minutes passed and then, at five to twelve, the clock stopped. The big minute hand did not move. We waited and waited, but nothing happened. Suddenly someone shouted, ‘It’s two minutes past twelve! The clock has stopped!’ I looked at my watch. It was true. The big clock refused to welcome the New Year. At that moment, everybody began to laugh and sing.
Joe Sanders has the most beautiful garden in our town. Nearly everybody enters for ‘The Nicest Garden Competition’ each year, but Joe wins every time. Bill Frith’s garden is larger than Joe’s. Bill works harder than Joe and grows more flowers and vegetables, but Joe’s garden is more interesting. He has made neat paths and has built a wooden bridge over a pool. I like gardens too, but I do not like hard work. Every year I enter for the garden competition too, and I always win a little prize for the worst garden in the town!
The plane was late and detectives were waiting at the airport all morning. They were expecting a valuable parcel of diamonds from South Africa. A few hours earlier, someone had told the police that thieves would try to steal the diamonds. When the plane arrived, some of the detectives were waiting inside the main building while others were waiting on the airfield. Two men took the parcel off the plane and carried it into the Customs House. While two detectives were keeping guard at the door, two others opened the parcel. To their surprise, the precious parcel was full of stones and sand!