foo

关于bind、call以及apply

我怕爱的太早我们不能终老 提交于 2020-02-08 03:13:12
call以及apply的用法 1、调用函数 function foo(n){ console.log(n); } foo(1); foo.call(null,2); foo.apply(null,[3]); 2、改变所调用的函数的内部的this值的指向 var a=1; function foo(b){ console.log(this.a+b); } foo(2); foo.call({a:3},4); foo.apply({a:5},[6]); 3、借用其他对象的方法 var arr=[12,34,6,32,12,43]; var ret=Math.max.apply(null,arr); console.log(ret); 4、把类数组转换成数组 var obj={0:'1',1:'2',length:2}; var ret=[].slice.call(obj); console.log(ret); 关于slice的用法 var arr=[12,23,45,87]; var arr1=arr.slice(0,2); console.log(arr1); var arr2=[23,56,76]; var ret=arr.slice.call(arr2,0,2); console.log(ret); var rets=[].slice.call(arr2,0,2);

18_python_类关系

一世执手 提交于 2020-02-06 22:41:25
一、类与类之间的关系 1、依赖关系 1 class Elephant: 2 3 def __init__(self, name): 4 self.name = name 5 6 7 def open(self, ref): # 想要的是一个冰箱。 是哪个冰箱没有制定 8 print("冰箱哥哥, 开门把") 9 ref.open_door() 10 11 def close(self, ref): # 依赖关系 12 print("冰箱哥哥, 我进来了。 关门把") 13 ref.close_door() 14 15 def jin(self): 16 print("进冰箱装自己") 17 18 class Refrigerator: 19 20 def open_door(self): 21 print("冰箱陌陌的打开了自己的门") 22 def close_door(self): 23 print("冰箱陌陌的关上了自己的门 ") 24 25 # class GaoYaGuo: 26 # def open_door(self): 27 # print("冰箱陌陌的打开了自己的门") 28 # def close_door(self): 29 # print("冰箱陌陌的关上了自己的门 ") 30 31 32 alex = Elephant("李杰") 33 bx1 =

C# 定时器保活机制引起的内存泄露问题

烂漫一生 提交于 2020-02-06 11:15:44
C# 中有三种定时器, System.Windows.Forms 中的定时器和 System.Timers.Timer 的工作方式是完全一样的,所以,这里我们仅讨论 System.Timers.Timer 和 System.Threading.Timer 1、定时器保活 先来看一个例子: class Program { static void Main(string[] args) { Start(); GC.Collect(); Read(); } static void Start() { Foo f = new Foo(); System.Threading.Thread.Sleep(5_000); } } public class Foo { System.Timers.Timer _timer; public Foo() { _timer = new System.Timers.Timer(1000); _timer.Elapsed += timer_Elapsed; _timer.Start(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { WriteLine("System.Timers.Timer Elapsed."); } ~Foo() {

js属性对象的hasOwnProperty方法

偶尔善良 提交于 2020-02-02 13:09:28
判断自生属性与继承性 function foo() { this.name = 'foo' this.sayHi = function () { console.log('Say Hi') } } foo.prototype.sayGoodBy = function () { console.log('Say Good By') } var myPro = new foo() console.log(myPro.name) // foo console.log(myPro.hasOwnProperty('name')) // true console.log(myPro.hasOwnProperty('toString')) // false console.log(myPro.hasOwnProperty('hasOwnProperty')) // fasle console.log(myPro.hasOwnProperty('sayHi')) // true console.log(myPro.hasOwnProperty('sayGoodBy')) // false console.log('sayGoodBy' in myPro) // true 判断自生属性是否存在 var o = new Object(); o.prop = 'exists'; function

RegExp-dotAll

那年仲夏 提交于 2020-01-24 00:07:00
//.不能匹配四个字节的utf16字符和行终止符\n,\r console.log(/foo.bar/.test('foo\nbar')) //false //dotAll console.log(/foo.bar/us.test('foo\nbar')) //true //如何判断正则是否启用了dotAll模式 const re = /foo.bar/s console.log(re.dotAll) //true console.log(re.flags) //s 来源: https://www.cnblogs.com/qjb2404/p/12231687.html

2019/12/30 11:23

放肆的年华 提交于 2020-01-22 07:45:14
var foo = [ ] ; foo [ 100 ] = 100 ; //console.log(foo) => [empty*100,100] foo.length = 101 for ( var i in foo ) { //前一百个都是空值 console . log ( "A" + i ) //=>A100 } 来源: CSDN 作者: Sword_meaning 链接: https://blog.csdn.net/Sword_meaning/article/details/103763449

Object.entries() 使用

☆樱花仙子☆ 提交于 2020-01-14 16:07:12
Object.entries() 方法返回一个给定对象自身可枚举属性的键值对数组。 其排列与使用 for...in 循环遍历该对象时返回的顺序一致(区别在于 for-in 循环也枚举原型链中的属性)。 Object.entries() 可以把一个对象的键值以数组的形式遍历出来,结果和 for...in 一致,但不会遍历原型属性。 示例1 -- 传入对象 const obj = { foo: 'bar', baz: 'abc' }; console.log(Object.entries(obj)); // [['foo', 'bar'], ['baz', 'abc']] 示例2.1 -- 数组 const arr = [1, 2, 3]; console.log(Object.entries(arr)); // [['0', 1], ['1', '2'], ['2', '3']] 示例2.2 -- 数组(数组中包含对象) const arr1 = [{ a: 1 }, 2, 3]; console.log(Object.entries(arr1)); // [['0', { a: 1 }], ['1', '2'], ['2', '3']] 示例2.3 -- 数组(数组中的值全部为对象) const arr2 = [{ a: 1 }, { b: 2 }, { c: 3 }];

python之(__getattribute__)

感情迁移 提交于 2020-01-14 05:45:26
1、回顾__getattr__ 当调用一个不存在的属性时,就会触发__getattr__() class Foo : def __init__ ( self , x ) : self . x = x def __getattr__ ( self , item ) : print ( '执行的是我' ) # return self.__dict__[item] f1 = Foo ( 10 ) print ( f1 . x ) f1 . xxxxxx # 不存在的属性访问,触发__getattr__ 结果: 10 执行的是我 2、__getattribute__ class Foo : def __init__ ( self , x ) : self . x = x def __getattribute__ ( self , item ) : print ( '不管是否存在,我都会执行' ) f1 = Foo ( 10 ) f1 . x # 不管是否存在,我都会执行 f1 . xxxxxx # 不管是否存在,我都会执行 3、二者同时出现 当__getattribute__与__getattr__同时存在,只会执行__getattribute__,除非__getattribute__在执行过程中抛出异常AttributeError #_*_coding:utf-8_*_ class

【ECMAScript5】Object对象

空扰寡人 提交于 2020-01-13 17:44:40
1. Object.create(prototype, [propertiesObject]) 使用指定的原型对象及其属性去创建一个新的对象。 var parent = { x : 1, y : 1 } var child = Object.create(parent,{ z : { // z会成为创建对象的属性 writable:true, configurable:true, value: "newAdd" } }); console.log(child); 2. Object.defineProperties(obj, props) 直接在一个对象上定义新的属性或修改现有属性,并返回该对象。 var obj = {}; Object.defineProperties(obj, { 'property1': { value: true, writable: true }, 'property2': { value: 'Hello', writable: false } }); console.log(obj); // {property1: true, property2: "Hello"} props有以下属性可以配置: configurable:true 当且仅当该属性描述符的类型可以被改变并且该属性可以从对应对象中删除。默认为 false。 enumerable:true

19面向对象--特殊成员

泪湿孤枕 提交于 2020-01-12 17:07:23
在python中,除了一些方法,属性之外,还有一些特殊成员 1 class Foo(object): 2 3 def __new__(cls, *args, **kwargs): 4 """ 5 创建一个空对象 6 :param args: 7 :param kwargs: 8 :return: 9 """ 10 print(2) 11 return object.__new__(cls) 12 13 def __init__(self, a1, a2): 14 # 为一个空对象进行数据的初始化 15 self.a1 = a1 16 self.a2 = a2 17 18 def __call__(self, *args, **kwargs): 19 print(1111) 20 21 def __getitem__(self, item): 22 print(item) 23 return 8 24 25 def __setitem__(self, key, value): 26 print(key, value, 1111111) 27 28 def __delitem__(self, key): 29 print(key) 30 31 def __add__(self, other): 32 return self.a1 + other.a1 33 34 def __enter