https://github.com/khajavi/javascriptnotes
Javascript Notes
https://github.com/khajavi/javascriptnotes
Last synced: 10 months ago
JSON representation
Javascript Notes
- Host: GitHub
- URL: https://github.com/khajavi/javascriptnotes
- Owner: khajavi
- Created: 2015-10-10T04:06:15.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-10-22T19:40:51.000Z (about 10 years ago)
- Last Synced: 2025-01-08T11:12:48.345Z (12 months ago)
- Homepage:
- Size: 129 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JavaScriptNotes
## Constructors
* built-in constructors: `Object`, `Array`, `Function`
```javascript
function Student() {
// empty
}
var student1 = new Student();
var student2 = new Student'
console.log(student1 instanceof Student); //true
console.log(student1.constructor === Student); //true
```
```javascript
function Student(name) {
this.name = name;
this.printName = function() {
console.log(this.name);
};
}
```
```javascript
function Student(name) {
Object.defineProperty(this, "name", {
get: function() {
return name;
},
set: function(newName) {
name = newName;
},
enumerable: true,
configurable: true
});
this.printName = function() {
console.log(this.name);
};
}
```
risk of changing global object:
```javascript
var student = Person("Milad"); // when constructor called without new
console.log(name); // "Milad"
```
## Prototypes
```javascript
var book = {
title: "JavaScript Book"
};
console.log("hasOwnProperty" in book); //true
console.log(book.hasOwnProperty("hasOwnProperty")); //false
console.log(Object.prototype.hasOwnProperty("hasOwnProperty")); //true
```
```javascript
var object = {};
var prototype = Object.getPrototypeOf(object);
console.log(prototype === Object.prototype); //true
```
# new String('blah blah') vs 'blah blah'
```
typeof new String('blah blah') === 'object'
typeof 'blah blah' === "string"
```