- var obj1 = new Object();
var obj2 = {}
- var obj3 = Object.create({});
- var obj1 = {
- name: "李四",
- age: 19
- };
- obj1.name = "张三";
- obj1.age = "20";
- obj1 = {
- height: "178cm",
- hobby: function() {
- console.log("喜欢篮球")
- }
- }
- var a = 10;
- var b = a;
- b = 5;
传址:对象赋值去相同的内存地址
复杂的数据类型传址
- var obj = {
- a: 10
- }
- var obj2 = obj;
- obj2.a = 5;
- console.log(obj)
var : 只有一层作用域
let : 在作用域内有效,
const :定义常量
- { //不再是一个单纯对象
- let a = 10;
- var b = 15;
- console.log(a, b)
- }
- console.log(a) //undefind
- console.log(b) //15
- const PI = "3.1419265897";
- console.log(PI);
- const PI = "3.1415926"; //重复定义常量会报错
ES5 写法
- let str = "姓名是" + name + "年龄是18";
- console.log(str);
ES6 写法
- let str = `姓名是${name},年龄是18岁`;
- console.log(str);
- var name = "库里";
- var pwd = "123456"
es5 写法
- var obj = {
- name: name
- }
es6 写法
- var obj = {
- name,
- pwd //键名,键值一样可以写成一个
- }
es5 写法
- var dog = {
- name: "小黑",
- sex: "male",
- action: function() {;
- console.log("吃、撒欢、叫")
- }
- }
es6 写法
- var dog = {
- name: "小黑",
- sex: "male",
- action() {;
- console.log("吃、撒欢、叫")
- }
- }
es5 写法
- var test1 = function(str) { //传参
- return str;
- }
- var test2 = function() { //无参数
- var str = "你好";
- return str;
- }
es6 写法 :箭头函数
- var test1 = (str) = >str //传参函数
- var test2 = () = >{ //无参函数
- var str = "你好";
- return str;
- }
注:对象的写法和匿名函数的写法很像
- var test = () = >({
- name: "历史",
- age: "18"
- }) //对象要加上(),不加,系统无法分清是作用域还是对象,会报错
- console.log(test());
- var obj = {
- name: "李四",
- age: "18",
- // action:function(){
- // console.log(this)
- // }
- //箭头函数写法:(this穿透)
- action: () = >{
- console.log(this)
- }
- }
工厂模式:避免变量污染
- function Factory(height) {
- //obj 类
- var obj = {};
- obj.name = "张三";
- obj.height = height;
- obj.age = "14";
- obj.hobby = function() {
- console.log("我喜欢游戏");
- }
- return obj;
- }
- var newFactory = Factory("179cm") console.log(newFactory.height);
- newFactory.hobby();
构造函数: (取代工厂函数)(类似于类的概念)
- function Dog(name, sex) {
- this.name = name;
- this.sex = sex;
- this.action = function() {
- console.log("叫、跑、吃")
- }
- }
调用构造函数
实例化 关键字 :new(吧抽象类具体化,吧类变成对象)
- var ahuan= new Dog("阿欢","公");
- ahuan.action();
- console.log(ahuan.name)
- var xiaohei = new Dog("小黑","母")
new 预定义:做了三件事
1、创建一个空对象
2、改变 this 指向
3、赋值原型;(构造函数原型赋给对象类型)
改变 this 指向:call、apply、bind
- function test(name, age) {
- console.log(name, age) console.log(this)
- }
- var obj = {
- name: "张三",
- age: 19
- }
- // this 指向的都是obj
- test.call(obj, "李四", 20);
- test.apply(obj, ["王五", 18]); //数组传参
- test.bind(obj)("马六", 20)
构造函数的原型;
对象是由自身和原型共同构成的;对象的原型是 proto
构造函数是由自身和原型共同构成的;构造函数的原型 prototype
属性写在构造函数里;方法写在原型上
- function Person(name, sex, age, height) {
- this.name = name;
- this.sex = sex;
- this.age = age;
- this.height = height;
- }
- Person.prototype.action = function() {
- console.log(this) console.log("跑步")
- }
- Person.prototype.hobby = function() {
- console.log("姓名是" + this.name) //实例化是谁就是谁
- console.log("喜欢游戏")
- }
- Person.prototype = function() {
- action: function() {
- console.log("跑步")
- }
- }
- var lisi = new Person("李四", "man", 18, "180cm") lisi.action();
- lisi.hobby()
- function Person(name) {
- //私有属性;
- var name = name;
- //公有属性
- this.height = "186cm";
- //get方法,通过公有方法访问私有属性
- this.get = function() {
- return name;
- }
- //set方法:设置私有属性;
- this.set = function(Newname) {
- name = Newname;
- console.log(Newname)
- }
- }
- var newPerson = new Person("张三");
- console.log(newPerson.get()) newPerson.set("凯基")
- function Human(sex, height) {
- this.sex = sex;
- this.live = "活着";
- this.height = height;
- this.action = function() {
- console.log("衣食住行");
- }
- }
- Human.prototype.hobby = function() {
- console.log("喜欢足球")
- }
- function Student(sex, height) {
- Human.call(this, sex, height);
- this.active = function() {
- console.log("学习")
- }
- }
- var newStudent = new Student("man", "180cm");
- newStudent.action(); // 衣食住行
- newStudent.hobby(); // 喜欢足球
- Student.prototype = Human.prototype;
- function Link() {} //另起一个空间
- Link.prototype = Human.prototype;
- Student.prototype = new Link();
- //重写子集的方法会影响父集
- Student.prototype.hobby = function() {
- console.log("喜欢下棋")
- }
来源: http://www.jianshu.com/p/39bfd2621ef1