avatarWendy Scott

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

8604

Abstract

<span class="hljs-keyword">const</span> boundFn = fn.<span class="hljs-title function_">bind</span>(<span class="hljs-variable language_">this</span>); <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperty</span>(<span class="hljs-variable language_">this</span>, key, { <span class="hljs-attr">value</span>: boundFn, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, }); <span class="hljs-keyword">return</span> boundFn; }, }; }

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyComponent</span> { <span class="hljs-title function_">constructor</span>(<span class="hljs-params">props</span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-property">props</span> = props; } @autobind <span class="hljs-title function_">handleClick</span>(<span class="hljs-params"></span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">props</span>); } }</pre></div><p id="0013">In this example, the decorator is used to decorate the function so that it automatically binds this when called and returns a new function. This way, after instantiating , you don’t need to manually bind this when calling the function.<code>autobindhandleClickMyComponentthis.handleClick()</code></p><h1 id="3ffe">Logging</h1><p id="7a28">Decorators can be used to record logs, including printing information such as function calls, function execution times, etc.</p><div id="1a70"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">log</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">Function <span class="hljs-subst">${name}</span> called with <span class="hljs-subst">${args}</span></span>); <span class="hljs-keyword">const</span> start = performance.<span class="hljs-title function_">now</span>(); <span class="hljs-keyword">const</span> result = originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); <span class="hljs-keyword">const</span> duration = performance.<span class="hljs-title function_">now</span>() - start; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">Function <span class="hljs-subst">${name}</span> completed in <span class="hljs-subst">${duration}</span>ms</span>); <span class="hljs-keyword">return</span> result; }; <span class="hljs-keyword">return</span> descriptor } <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyClass</span> { @log <span class="hljs-title function_">myMethod</span>(<span class="hljs-params">arg1, arg2</span>) { <span class="hljs-keyword">return</span> arg1 + arg2; } }

<span class="hljs-keyword">const</span> obj = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyClass</span>(); obj.<span class="hljs-title function_">myMethod</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <span class="hljs-comment">// Output: </span> <span class="hljs-comment">// Function myMethod called with 1,2</span> <span class="hljs-comment">// Function myMethod completed in 0.013614237010165215ms</span></pre></div><h1 id="aff7">Authentication authentication</h1><p id="bbff">Decorators can also be used to check a user’s authentication status and permissions to prevent unauthorized users from accessing sensitive data or performing actions on a regular basis.</p><div id="be1a"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">authorization</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">...args</span>) { <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">isAuthenticated</span>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Access denied! Not authenticated'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">hasAccessTo</span>(name)) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">Access denied! User does not have permission to <span class="hljs-subst">${name}</span></span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); }; <span class="hljs-keyword">return</span> descriptor; }

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyApi</span> { <span class="hljs-title function_">isAuthenticated</span>(<span class="hljs-params"></span>) { <span class="hljs-comment">// perform authentication check</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } <span class="hljs-title function_">hasAccessTo</span>(<span class="hljs-params">endpoint</span>) { <span class="hljs-comment">// perform authorization check</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } @authorization <span class="hljs-title function_">getUsers</span>(<span class="hljs-params"></span>) { <span class="hljs-comment">// return users data</span> } @authorization <span class="hljs-title function_">deleteUser</span>(<span class="hljs-params">id</span>) { <span class="hljs-comment">// delete user with id</span> } }</pre></div><h1 id="646e">Cache</h1><p id="3465">Decorators can also be used to cache the execution results of functions to avoid double evaluation.</p><div id="5257"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">memoize</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>; <span class="hljs-keyword">const</span> cache = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>();

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { <span class="hljs-keyword">const</span> cacheKey = args.<span class="hljs-title function_">toString</span>(); <span class="hljs-keyword">if</span> (cache.<span class="hljs-title function_">has</span>(cacheKey)) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">cache hit: <span class="hljs-subst">${cacheKey}</span></span>); <span class="hljs-keyword">return</span> cache.<span class="hljs-title function_">get</span>(cacheKey); } <span class="hljs-keyword">const</span> result = originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">cache miss: <span class="hljs-subst">${cacheKey}</span></span>); cache.<span class="hljs-title function_">set</span>(cacheKey, result); <span class="hljs-keyword">return</span> result; }; <span class="hljs-keyword">return</span> descriptor; }

<span class="hljs-keyword">class

Options

</span> <span class="hljs-title class_">MyMath</span> { @memoize <span class="hljs-title function_">calculate</span>(<span class="hljs-params">num</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'calculate called'</span>); <span class="hljs-keyword">return</span> num * <span class="hljs-number">2</span>; } }

<span class="hljs-keyword">const</span> math = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyMath</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-title function_">calculate</span>(<span class="hljs-number">10</span>)); <span class="hljs-comment">// Output: </span> <span class="hljs-comment">// calculate called</span> <span class="hljs-comment">// cache miss: 10</span> <span class="hljs-comment">// 20</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-title function_">calculate</span>(<span class="hljs-number">10</span>)); <span class="hljs-comment">// Output: </span> <span class="hljs-comment">// cache hit: 10</span> <span class="hljs-comment">// 20</span></pre></div><h1 id="57d9">Aspect-oriented programming</h1><p id="0d61">Decorators can be used to implement facet-oriented programming, that is, to add functionality at runtime without modifying the original code.</p><div id="bc4d"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">validate</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { <span class="hljs-keyword">const</span> isValid = args.<span class="hljs-title function_">every</span>(<span class="hljs-function"><span class="hljs-params">arg</span> =></span> <span class="hljs-keyword">typeof</span> arg === <span class="hljs-string">'string'</span> && arg.<span class="hljs-property">length</span> > <span class="hljs-number">0</span>); <span class="hljs-keyword">if</span> (!isValid) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Invalid arguments'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); }; <span class="hljs-keyword">return</span> descriptor; }

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyForm</span> { @validate <span class="hljs-title function_">submit</span>(<span class="hljs-params">name, email, message</span>) { <span class="hljs-comment">// submit the form</span> } }

<span class="hljs-keyword">const</span> form = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyForm</span>(); form.<span class="hljs-title function_">submit</span>(<span class="hljs-string">''</span>, <span class="hljs-string">'[email protected]'</span>, <span class="hljs-string">'Hello world'</span>); <span class="hljs-comment">// Output: Invalid arguments</span></pre></div><h1 id="c8da">Reversible decorators</h1><p id="5322">Decorators can also be applied in reversible scenes, for example, you can add a reversible decorator to modify function behavior.</p><div id="b868"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">reverse</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { args.<span class="hljs-title function_">reverse</span>(); <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); }; <span class="hljs-keyword">return</span> descriptor; } <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyMath</span> { @reverse <span class="hljs-title function_">calculate</span>(<span class="hljs-params">num1, num2</span>) { <span class="hljs-keyword">return</span> num1 + num2; } }

<span class="hljs-keyword">const</span> math = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyMath</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-title function_">calculate</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)); <span class="hljs-comment">// Output: 3</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-property">calculate</span>.<span class="hljs-title function_">reversed</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)); <span class="hljs-comment">// Output: 3</span></pre></div><h1 id="2ddc">Automatic type checking</h1><p id="037c">Decorators can be applied to automatic type checking, for example, you can add a decorator to ensure that the type of a function parameter is correct.</p><div id="a63a"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">checkType</span>(<span class="hljs-params">expectedType</span>) { <span class="hljs-keyword">return</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) {
      <span class="hljs-keyword">const</span> invalidArgs = args.<span class="hljs-title function_">filter</span>(<span class="hljs-function"><span class="hljs-params">arg</span> =&gt;</span> <span class="hljs-keyword">typeof</span> arg !== expectedType);
      <span class="hljs-keyword">if</span> (invalidArgs.<span class="hljs-property">length</span> &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`Invalid arguments: <span class="hljs-subst">${invalidArgs}</span>`</span>);
        <span class="hljs-keyword">return</span>;
      }
      <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args);
    };
    <span class="hljs-keyword">return</span> descriptor;
  }

}

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyMath</span> { @<span class="hljs-title function_">checkType</span>(<span class="hljs-string">'number'</span>) <span class="hljs-title function_">add</span>(<span class="hljs-params">num1, num2</span>) { <span class="hljs-keyword">return</span> num1 + num2; } }

<span class="hljs-keyword">const</span> math = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyMath</span>(); math.<span class="hljs-title function_">add</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'2'</span>); <span class="hljs-comment">// Output: Invalid arguments: 2</span></pre></div><p id="dd86"><i>More content at <a href="https://plainenglish.io/"><b>PlainEnglish.io</b></a>.</i></p><p id="5cf7"><i>Sign up for our <a href="http://newsletter.plainenglish.io/"><b>free weekly newsletter</b></a>. Follow us on <a href="https://twitter.com/inPlainEngHQ"><b>Twitter</b></a></i>, <a href="https://www.linkedin.com/company/inplainenglish/"><b><i>LinkedIn</i></b></a><i>, <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><b>YouTube</b></a>, and <a href="https://discord.gg/GtDtUAvyhW"><b>Discord</b></a><b>.</b></i></p><p id="7057"><b><i>Interested in scaling your software startup</i></b><i>? Check out <a href="https://circuit.ooo?utm=publication-post-cta"><b>Circuit</b></a>.</i></p></article></body>

5 Questions to Ask the Senior Team When Designing a Leadership Program

Match your leadership program to the learners, the budget, and the situation

QQPhoto by Redd F on Unsplash

Three Minute Train the Trainer Series #21

When you’re designing a leadership program, how do you go about it?

What does a good leadership development program look like?

What do you put in the leadership program?

How do you even write a development program to please the executive team and the learners?

“I start with the premise that the function of leadership is to produce more leaders, not more followers.” -Ralph Nader

I’ve had to do this several times, and the leadership programs I’ve designed have always been slightly different to suit the circumstances, the learners, and the available budget.

When developing a leadership program, you need to know the high-level purpose of the training.

Remember that your vision of leadership training will likely differ from other leaders, the senior team, and the CEO, so you need to reach a consensus before you’ve done too much work.

Before you start scoping out a program, organize a meeting with the decision-makers and find out what solution is most likely to go ahead.

If you can get all of the senior team on board from the beginning, you are much more likely to be allocated the budget, time, and other resources you’ll need.

Otherwise, you could spend a lot of time designing a program that never sees the light of day.

I learned this early in my career when I developed a customer services program for a senior manager. I worked hard on the program, only to discover that he hadn’t consulted the rest of the senior team, and the course never went ahead.

Now that I’m older and wiser, I only write something with an approval from the top.

When designing a leadership development program, asking the following questions is a good start.

One: Who are the leaders in the organization?

Your organization might have leading hands, team leaders, 2ICs, 3ICs, supervisors, managers, managers of managers, senior execs, and professional leaders with no direct reports.

It’s important to know which leaders will get the training. Is it all leaders or only some of them?

Will you start with one group and move on to the others?

“A leader is the one in the charge, the person who convinces other people to follow. A great leader inspires confidence in other people and moves them to action.” — www.vocabulary.com

If one group, such as supervisors, is selected, will people who stand in as supervisors or who are being groomed to be supervisors attend as well?

Find out if everyone agrees on who is a leader and what a leader does.

Two: What is the purpose of the leadership program?

Why do you need a leadership program?

Do the leaders need to be more proactive?

Do they need training on people skills and leading a team?

Do they need to consider the bigger picture or be innovative?

Make sure that it’s a leadership program that is required.

For example, the main issue is that some managers are not managing sick leave. In that case, you need a sick leave management program, not a leadership program.

Photo by Campaign Creators on Unsplash

Three: What do you want your leaders to do daily, weekly, and monthly?

For each leadership role, what are they expected to achieve?

For example, are they supposed to recruit and interview new staff, onboard new staff effectively, and write reports and proposals?

Do they need to bring in new business? Are these tasks aligned with their existing position descriptions, or do you need a restructure with new roles and responsibilities?

Consider whether the expectations of the roles have changed since the roles were originally created.

If the senior team has a high turnover, they may not realize their idea of the role is different from what the incumbents were hired to do.

A review of expectations matched to position descriptions is sometimes an eye-opener.

Four: How do you want the leaders to behave?

Do you want them to wear suits, behave formally, and direct the team or muck in with the day-to-day work when needed?

Are they expected to attend organizational social events and out-of-hours training? Entertain clients?

What values are they expected to adhere to? How should they treat their teams?

“In matters of style, swim with the current; in matters of principle, stand like a rock.” — Thomas Jefferson

Remember that you give mixed messages if you say you want honesty, integrity, and manners but reward bullies who get results.

Or say you value work-life balance and wellness but expect your leaders to work excessively late and at weekends.

Photo by Brett Jordan on Unsplash

Five: What do the leaders need to be able to do at the end of the training that they couldn’t do at the beginning?

Again, dive into the details.

When your senior team says they want better people skills, find out what they mean because it could be anything from saying hello to everyone in the morning to becoming qualified in a personality assessment tool.

Ensure that you include any required leadership tasks in the training, and everyone is agreed on what those tasks are.

For example, leaders typically interview, select, onboard, train, and motivate their teams. Does everyone agree that these topics should be included?

If leaders must develop their teams, include how to make a development plan in the training.

Get specifics, not generalities.

Before the meeting, do a bit of research and make a list of leadership skills so you can throw them all in the mix while you’ve got the senior team on hand.

What next?

Once you’ve got all this information, you can start scoping out a business proposal for approval containing:

  • Why the training is needed
  • High-level objectives of the training
  • Which roles will attend
  • The training content for each role
  • Options for programs with and without qualifications
  • Options for internal programs or those provided by external consultants
  • The budget required for the different options included the trainee’s wages
  • Timescales for rollout
  • Course duration
  • Options for program deployment such as full days, part days, online or Face-to-face, or hybrid

The proposal contains more detail and needs to be signed off by the senior team, including the CEO or MD, especially regarding the budget.

The original meeting was to ensure the organization has an appetite for leadership training and that everyone agrees on what that looks like.

In contrast, the business proposal gives more detail about the logistics if the leadership development program goes ahead.

Within the proposal, you can give recommendations, pros and cons, and options for staggered implementation. What you are providing are possibilities together with information about the resources required.

If you take the time to scope out options and the cost first, you’ll avoid wasting time.

Designing an extensive training program takes time, but proper planning will pay off in the long run.

Want to learn more? Click here to subscribe to my weekly newsletter — Get all my latest articles straight to your inbox and a free copy of The New Leader’s Starter Kit.

Leadership Development
Learninganddevelopment
Training Courses
Training
Management And Leadership
Recommended from ReadMedium