https://github.com/webengage/babel-plugin-shield
A babel plugin to shield code against undefined properties
https://github.com/webengage/babel-plugin-shield
Last synced: 8 months ago
JSON representation
A babel plugin to shield code against undefined properties
- Host: GitHub
- URL: https://github.com/webengage/babel-plugin-shield
- Owner: WebEngage
- License: mit
- Created: 2016-11-07T07:15:02.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-07T07:51:02.000Z (over 9 years ago)
- Last Synced: 2025-01-14T18:10:32.140Z (over 1 year ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# babel-plugin-shield
A babel plugin to shield your code against annoying undefined property errors (```Uncaught TypeError: Cannot read property 'xyz' of undefined"```).
Let's say you have a deep nested object something like this:
```javascript
var testObj = {
func1: function () {
return { b : 2 };
},
testFunc:function(){
var value = this.func2().b; // undefined property func2
}
}
```
So as you would have guessed correctly this would through our not so favourite undefined error.
till now most of you would have been solving this problem using ```&&``` something like,
```javascript
var value = this && this.func2 && this.func2() && this.func2().b;
```
Well, not anymore this plugin is here to **shield** you against all such troubles.
### Getting Started
```javascript
$ npm install --save babel-plugin-shield
//add shield as plugin in your babelrc
{
plugins: ['shield']
}
```
### Usage
```javascript
var value = shield_(this.func2().b);
```
That's pretty much it.
### License
babel-plugin-shield is released under the [MIT License](http://www.opensource.org/licenses/MIT)
### Thanks