草庐IT

private_count

全部标签

javascript - AngularJS 指令中的单元测试私有(private)函数

如何对在指令内定义的函数进行单元测试,如下面的myFunc?angular.module('myApp').directive('myDir',[function(){varmyFunc=function(arg){//codeinhere.};return{restrict:'A',scope:{},link:function(scope,element){}};}]);或者您如何定义我不想在指令之外公开的可测试指令特定函数? 最佳答案 最常见的方法是不测试私有(private)方法,而是测试公开其行为的公共(public)接口(

javascript - 重访 Python 私有(private)实例数据

我读过各种“Python实例中没有真正私有(private)数据”的帖子,但我们都知道在Perl和JavaScript中使用闭包来有效实现私有(private)数据。那么为什么不用Python呢?例如:importcodecsclassSecret:def__private():secret_data=Nonedef__init__(self,string):nonlocalsecret_dataifsecret_dataisNone:secret_data=stringdefgetSecret(self):returncodecs.encode(secret_data,'rot_13

javascript - 为什么谷歌的闭包库不使用真正的私有(private)成员?

我成为JavaScript开发人员已有一段时间了,我一直认为在JavaScript中实现私有(private)成员的正确方法是使用DougCrockford在此处概述的技术:http://javascript.crockford.com/private.html.在我开始使用GoogleClosure库之前,我不认为这是一个特别有争议的JavaScript智慧。想象一下我的惊讶......图书馆没有努力使用Crockford风格的信息隐藏。他们所做的只是使用特殊的命名约定并在文档中注明“私有(private)”成员。我习惯于假设Google的人通常处于软件质量的领先地位,那又如何呢?遵

Javascript/JQuery : How do I count the words separated with a comma?

Javascript:$(document).ready(function(){$('#field').keyup(function(){varcount='??';$('#count').html(count);});});HTML:5示例(单词总是用逗号分隔):example1:word,wordwordcount:(5-2)=3example2:wordcount:(5-1)=4example3:word,word,count:(5-2)=3example4:word,word,wordcount:(5-3)=2因此,我需要计算有多少个单词以逗号分隔,但例如示例3中所示,不应将它

javascript - 访问原型(prototype)中的 "private"变量

是否可以在JavaScript中创建一个可以在原型(prototype)中访问的私有(private)变量?我尝试了以下obviouslydoesn'twork,因为bar只能从Foo中访问,而不能从原型(prototype)中访问。functionFoo(){varbar='test';}Foo.prototype.baz=function(){console.log(bar);};我知道我也不能使用this.bar='test',因为那样会使thepropertypublic据我所知。如何使bar变量私有(private),但可由原型(prototype)访问?

javascript - 在 TypeScript 中编写闭包

只是为了好玩,我正在尝试在包含闭包的代码中使用TypeScript的强类型。原始JavaScript代码如下所示:varfunc=(function(){varprivate_count=0;varincrement=function(){private_count+=1;returnprivate_count;}returnincrement;}());这是我用TypeScript实现它的最佳尝试:varfunc:()=>()=>number=(function():()=>number{var_count:number=0;varincrement:()=>number=funct

javascript - 在对显示模块进行单元测试时如何 stub 私有(private)函数

我一直在构建一个Node模块,它包装了对GitHubAPI的大量调用,并且以我无限的智慧使用揭示模块模式构建了这个模块,使我的包装函数保持私有(private)并且只公开简单的方法。请参见下面的示例:github.shortcuts=(function(){varappPath;varcreateRepo=function(name){vardeferred=Q.defer();github.repos.create({name:name,auto_init:true},function(error,result){if(error){deferred.reject(newError(

私有(private)属性的 Javascript 动态 getter/setter

我想动态地创建getter/setter方法来检索私有(private)属性。这是我做的。首先,我制作了这个类:functionwinClass(posX,posY,w,h){varx=posX||0;vary=posY||0;varwidth=w||0;varheight=h||0;}然后我用getter/setter方法扩展了winClass,如下:winClass.prototype.getX=function(){returnx;}winClass.prototype.setX=function(val){x=val;}然后我测试了:varwin1=newwinClass(10

javascript - jquery count siblings 不返回正确的结果?

我想统计sibling按类,html,12345​j查询,varlen=$('.item-sibling').siblings().css({background:'red'}).length;alert(len);​​​​//return4它不包括1我怎样才能包含它?jsfiddlelink如果我将html更改为,012345我会得到6这次。奇怪!编辑,1  2  3  4  5​1  2  3有一系列具有相同类别的组,我想动态计算目标组的sibling,例如第一组。 最佳答案 你可以做到varlen=$('.item-sibli

javascript - Mootools 类中的私有(private)方法

我对在Javascript中使用oop比较陌生,我想知道私有(private)方法的最佳实践是什么。现在,我正在使用mootools创建我的类,并通过在私有(private)方法前加上下划线并强制自己不要在类外部调用该方法来模拟私有(private)方法。所以我的课看起来像:varNotifier=newClass({...showMessage:function(message){//publicmethod...},_setElementClass:function(class){//privatemethod...}});这是在JS中处理私有(private)方法的良好/标准方式