关于javascript的一些知识

欢迎来观看

哈哈,这是我最新的javascript的感悟,请大家多多指教,其中关于我认为还比较容易忘的地方,特地记录下来

object 和 function 的不同

google 解释: JavaScript functions are a special type of objects, called function objects. A function object includes a string which holds the actual code – the function body – of the function. The code is literally just a string.
这意思就是说function 是object 的 一种,没什么特别的区别,用String进行存储,

class和object的区别

Class 定义:
Classes are in fact “special functions”, and just as you can define function expressions and function declarations, the class syntax has two components: class expressions and class declarations.
example;
  function Cat() {

this.name = “大毛”;

}
   var cat1 = new Cat();

alert(cat1.name); // 大毛
参考link:
http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html
http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html

这种方法,我们可以看到有this, 与函数的区别在于使用了new, 然后()不再是invoke用的,其实这有个名字叫做构造函数,是特殊的函数
类的构造方法:
  Cat.prototype.makeSound = function(){

alert(“喵喵喵”);

}

Class 和Object 的关系:
A class is a template for objects. A class defines object properties including a valid range of values, and a default value. A class also describes object behavior. An object is a member or an “instance” of a class.
我总觉得只需要object 就够了,class是object 一个省力气的方法,用就好了,强行区分反而有问题
,function 是一个object, var b=function(){}, 本来我们用变量都是直接a=b, c=b之类的,但是如果我们想要invoke,就必须b();

Namespace

这个是解决不同JavaScript file命名相同的变量,导致的问题,
方法是用object 来包裹所有的变量和函数, var object ={}
object.function ={}
object.var=…
然后就可以避开这个问题了

Closure

closure means it will still remain the space and memory,就是function a1( global ){
return{
function b(x){
x*global}
}
}

Immediatedly invoked Function

(function (name){ console.log("Hello "+name)})()-> it will invoke
and we can export the local one to global one, using (function(window){
var a=1;window.a=a;})(window) //so the it will changed to window object;
就是不要把相同的变量名暴露在window class 里面

暂时写到这了,大家加油一起学

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值