Git Product home page Git Product logo

javascript-oo's Introduction

构造函数

function People(name){
	this.name = name;
}
var person = new People('Cassie Xu');
console.log(person.name);
function People(name) {
  this.name = name;
  this.greet = function() {
    console.log('Hello, my name is ' + this.name + '!');
  };
}
var person = new People('Cassie Xu');
person.greet();

原型

function People(name) {
  this.name = name;
}
People.prototype = {
  greet: function() {
    console.log('Hello, my name is ' + this.name + '!');
  }
};
var person = new People('Cassie Xu');
person.greet();
console.log(person.__proto__ === People.prototype); // true
var a = { foo: 1 };
var b = { bar: 2 };
b.__proto__ = a;
console.log(b.foo); 
console.log(b.bar); 
var a = {};
console.log(a.__proto__); // {}
console.log(a.__proto__.__proto__); // null

继承

function Parent(){}
Parent.prototype = {
	greet: function(){
	console.log('hello world');
}
}
function Child(){}
Child.prototype = new Parent();
var c = new Child();
c.greet(); //console.log('hello world');
function Parent() {
	this.name = 'xxx',
	this.date = 'xxx'
}
Parent.prototype.greet = function() {
  console.log('JavaScript rocks');
}

function Child() {}
Child.prototype = Object.create(Parent.prototype);
//硬记的知识	
Child.prototype.constructor = Child;
var child = new Child();
child.greet();

this

var person = {
    firstName: "Penelope",
    lastName: "Barrymore",
    sayFullName: function () {
        console.log(this.firstName + " " + this.lastName); //=> "Penelope Barrymore"
        console.log(person.firstName + " " + person.lastName); //=> "Penelope Barrymore"
    }
};
person.sayFullName();
var firstName = 'John',
  	lastName = 'Wu';
var person = {
    firstName: "Penelope",
    lastName: "Barrymore",
    sayFullName: function () {
        console.log(this.firstName + " " + this.lastName); 
    }
};
person.sayFullName(); //=> "Penelope Barrymore"
var sayFullName = person.sayFullName;
sayFullName(); // window is calling it!
funtion foo(){
	'use strict';
	console.log(this) //undefined
}

自执行函数

(function(c){
	var a = 1;
	var b = 2;
	console.log(a+b+c);
})(3)
// c = 3

闭包

var a = {};
a.foo = function(callback) {
  // do something and call callback in whatever way
}
a.bar = function() {
  this.num = 1;
  var that = this;
  //闭包,这里that可以访问到a.bar的作用域
  this.foo(function(newNum) {
    that.num = newNum;
  });
  console.log(this.num);
}
a.bar();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.