Littlombie's Nirvana

Novelty is the great parent of pleasure.


  • Home

  • Archives53

  • Tags31

  • About

  • Nirvana

javascript中定义对象的几种方式

Posted on 2016-11-25 | Edited on 2016-12-08 | In javascript

JavaScript中没有类的概念,只有对象。
在JavaScript中定义对象可以采用以下几种方式:
1.基于已有对象扩充其属性和方法
2.工厂方式
3.构造函数方式
4.原型(“prototype”)方式
5.动态原型方式

基于已有对象扩充其属性和方法

1
2
3
4
5
6
7
8
9
10
11
12
    var object = new Object();
object.name = "zhangsan";
object.sayName = function(name){
this.name = name;
alert(this.name);
}
object.sayName("lisi");
<<<<<<< HEAD
=======

>>>>>>> update hexo
`

这种方式的弊端:这种对象的可复用性不强,如果需要使用多个对象,还需要重新扩展其属性和方法。

Read more »

JavaScript 中的 this 详解

Posted on 2016-11-25 | In javascript

关于 js 中的 this 文章的一个总结归纳

在 js 中,this 这个上下文总是变化莫测,很多时候出现 bug 总是一头雾水,其实,只要分清楚不同的情况下如何执行就 ok 了。

全局执行

首先,我们在全局环境中看看它的 this 是什么:

  1. 浏览器:
1
2
3
console.log(this);

// Window {speechSynthesis: SpeechSynthesis, caches: CacheStorage, localStorage: Storage, sessionStorage: Storage, webkitStorageInfo: DeprecatedStorageInfo…}
  1. node:
1
2
3
console.log(this);

// global

可以看到打印出了 global 对象;

总结 :在全局作用域中它的 this 执行当前的全局对象(浏览器端是 Window,node 中是 global)。

Read more »

深入javascript——构造函数和原型对象

Posted on 2016-11-18 | In javascript

常用的几种对象创建模式

  • 使用new关键字创建

最基础的对象创建方式,无非就是和其他多数语言一样说的一样:没对象,你new一个呀!

1
2
3
4
5
6
var gf = new Object();
gf.name = "zhangSan";
gf.bar = "c++";
gf.sayWhat = function() {
console.log(this.name + "said:love you forever");
}

  • 使用字面量创建

这样似乎妥妥的了,但是宅寂的geek们岂能喜欢如此复杂和low土的定义变量的方式,作为一门脚本语言那应该有和其他兄弟们一样的范儿,于是出现了对象字面量的定义方式:

1
2
3
4
5
6
7
var gf = {
name : "zhangSan",
bar : "c++",
sayWhat : function() {
console.log(this.name + "said:love you forever");
}
}
Read more »

周末随拍(2016.11.13)

Posted on 2016-11-16 | In 个人随写
Read more »
1…101112…14
Littlombie

Littlombie

Said less to do more!

53 posts
11 categories
31 tags
RSS
GitHub Weibo Zhihu webSite
Creative Commons
© 2020 Littlombie
Powered by Hexo v3.9.0
|
Theme – NexT.Mist v6.4.2